LoginSignup
2
3

More than 5 years have passed since last update.

os.chdir() とスコープ

Posted at

Python でのカレントディレクトリの移動について。
説明のためカレントディレクトリは C:\ とします。(お察しの通り Windows です)

hoge.py
import os

def hoge():
    print('hoge')
    os.chdir(r'C:\foo\bar')

print(os.getcwd())
hoge()
print(os.getcwd())

os.getcwd()os.chdir() の説明は割愛。
出力は以下の通り。

Python3
>>> import hoge
C:\
hoge
C:\foo\bar

というわけでスコープを抜けてもカレントディレクトリは戻りませんでした。
個人的には呼び出した関数がなにしてるかなんて知らなくて済む方がいいと思うので、
元に戻してくれた方が親切な気がするんですが。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3