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')