How do I change the working directory in Python?

前端 未结 14 1153
天涯浪人
天涯浪人 2020-11-22 01:59

cd is the shell command to change the working directory.

How do I change the current working directory in Python?

14条回答
  •  礼貌的吻别
    2020-11-22 02:17

    import os
    
    abs_path = 'C://a/b/c'
    rel_path = './folder'
    
    os.chdir(abs_path)
    os.chdir(rel_path)
    

    You can use both with os.chdir(abs_path) or os.chdir(rel_path), there's no need to call os.getcwd() to use a relative path.

提交回复
热议问题