在项目根目录新建一个demo.py
文件,然后添加以下脚本,执行即可
#!/usr/bin/env python3 # -*- coding:utf-8 -*- import os import site BASE_DIR = os.getcwd() USER_PTH = os.path.join(site.getsitepackages()[-1], 'requirements.pth') def main(): try: os.remove(USER_PTH) except FileNotFoundError: pass with open(USER_PTH, 'w') as f: f.write(BASE_DIR) print("生成文件成功!") print("文件位置:%s" % USER_PTH) with open(USER_PTH) as f: print("文件内容:", f.read())
if __name__ == '__main__': main()
执行结果:
生成文件成功! 文件位置:D:\Python3\lib\site-packages\requirements.pth 文件内容: C:\Users\hoou\PycharmProjects\blog_project