Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我试图查看是否有任何方法可以写入正在执行代码的当前文件。
这是我写的代码:
with open(__file__, 'r+') as f: f.write('abc')
这没用。有什么方法可以使用 python 写入正在执行代码的文件?
我无法重现这个。我的文件test.py在执行后是
test.py
abch open(__file__, 'r+') as f: f.write('abc')
这告诉我该程序“按设计”运行。
尝试附加文件:
with open(__file__, 'a') as f: f.write('abc')