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.
Pythonスクリプト内から現在のランタイムディレクトリを見つけるにはどうすればよいですか?
を使用しos.getcwdます。これにより、現在の作業ディレクトリがわかります。で新しいファイルを作成する場合
os.getcwd
fp = open('a.txt', 'w')
その後a.txt、現在の作業ディレクトリに作成されます。os.chdir必要に応じて、作業ディレクトリを変更するために使用します。
a.txt
os.chdir
これは私のために働いています。
os.path.dirname(__file__)
os.getcwd()使用すると変化しますos.chdir
os.getcwd()
Pythonファイルへの固定パスが必要な場合は、次を使用します。
module_path = os.path.dirname(__file__)