One thing you need to keep in mind is that when using os.chdir to change the working directory of current programme (script), make sure if we need to come back to the current working directory again.

Using os.chdir to change the current working directory anywhere in the programme will effect permanently, regardless if you are using it in a function or not, see the following example:

 import os

 def chdir_in_func():
print "Before chaning, in func"
print os.getcwd()
os.chdir("/home/")
print "After chaning, in func"
print os.getcwd() if __name__ == "__main__":
print "Before changing, outside func"
print os.getcwd()
chdir_in_func()
print "After changing, outside func"
print os.getcwd()

The output of this script will be:

Before changing, outside func
/home/chdir_in_func
Before chaning, in func
/home/chdir_in_func
After chaning, in func
/home
After changing, outside func
/home

We can see that in the function, if we changes the directory, the current working directory is changed too.

A suggested good practice to avoid such unconsciously mistake is that alway change back to your original working directory after the actions done in the new directory, unless you know you need to stay there for next operations. I would strongly recommend that in functions, if you have to change directory, always changes it back before returning:

def func():
cwd_save = os.getcwd()
os.chdir(new_dir)
# Actions in new_dir
os.chdir(cwd_save)
return True

[Python] Pitfalls: Be Careful with os.chdir的更多相关文章

  1. 【转载】路径双反斜杠!!!Python IDLE或Python shell中切换路径 切换目录os.chdir("C:\\python37\\2019pythonshel37\\diedai")

    Python IDLE或shell中切换路径在Python自带的编辑器IDLE中或者python shell中不能使用cd命令,那么跳到目标路径呢.方法是使用os包下的相关函数实现路径切换功能. im ...

  2. Python os.chdir() 方法

    概述 os.chdir() 方法用于改变当前工作目录到指定的路径. 语法 chdir()方法语法格式如下: os.chdir(path) 参数 path -- 要切换到的新路径. 返回值 如果允许访问 ...

  3. python os.chdir() 用法

    概述 os.chdir() 方法用于改变当前工作目录到指定的路径. 语法 chdir()方法语法格式如下: os.chdir(path) 参数 path -- 要切换到的新路径. 返回值 如果允许访问 ...

  4. Python读写文件的路径,关于os.chdir(path)位置对程序的影响,

    关于os.chdir(path)位置对程序的影响,import os import time#直接把path放到open()里面 def fu0(): star = time.time() for i ...

  5. python常用模块json、os、sys

    一.序列化 json & pickle 模块 json--用于字符串和Python数据类型间进行转换 pickle---用于python特有的类型和Python的数据类型间进行转换 json: ...

  6. python中模块sys与os的一些常用方法

    sys模块提供了访问或操作与python解释器相关方法与对象. 我们就列举出常用到的知识,以后,随着学习,不断补充. 几个常用到的动态对象: sys.argv,这是一个列表,它包含了所有传递给脚本的命 ...

  7. Python(正则 Time datatime os sys random json pickle模块)

    正则表达式: import re #导入模块名 p = re.compile(-]代表匹配0至9的任意一个数字, 所以这里的意思是对传进来的字符串进行匹配,如果这个字符串的开头第一个字符是数字,就代表 ...

  8. Python:time模块/random模块/os模块/sys模块

    time 模块 #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time() 获取当前时间戳 python中时间日期格式化符号: %y 两位数的 ...

  9. Python函数参数&time、OS、json模块

    ##可变参数 PORT = 3306 #常量 def mysql(host,user,password,port,charset,sql,db): print('连接mysql') # mysql(' ...

随机推荐

  1. WIN32API 自定义颜色下拉列表控件

    效果如下: 原创-转载请著名来源 1,新建颜色属性类“CNColor”: class CNColor { public: COLORREF m_crColor; //颜色RGB值 WCHAR m_cC ...

  2. JSP开发模式2(计算器)

    CalculatorBean———————————————————————————————————————————— public class CalculatorBean {    private ...

  3. C语言file相关函数学习

    1.errno_t fopen_s( FILE** pFile, const char *filename, const char *mode ); 注:fopen_s能过创建文件,但无法创建目录 打 ...

  4. 修改Arduino串口缓冲区大小(转)

    本帖节选自<Arduino程序设计基础>第二版5.1.6串口缓冲区       在之前的示例程序中,我们都是采用人工输入测试数据的方式检验程序效果,Arduino每接收到一次数据,就会将数 ...

  5. The Zero

    TOday is just a alpha, but there isnot 欧米伽. 编程,是你选的,是你学的,也是你喜欢的,更是你追求的.为什么要放弃塔!. 加油! 为自己. #include&l ...

  6. C#窗体技巧

    //限制文本框只能输入数字且允许按退格键删除数字,其它键盘输入不予显示private void 文本框名_KeyPress(object sender, KeyPressEventArgs e)  { ...

  7. Android 开发环境配置

    转至:http://www.cnblogs.com/shangdahao/archive/2013/04/17/3025429.html Windows下的开发环境需要安装以下软件: Java JDK ...

  8. Git学习(二)——创建版本库、查看与回退版本

    一.创建版本库 版本库,又名仓库(Repository),可以简单理解为一个目录,这个目录里的所有文件可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者将来某 ...

  9. maven css/js 压缩配置

    <plugin>               <groupId>net.alchim31.maven</groupId>               <art ...

  10. JavaScript依赖注入的实现思路

    JavaScript依赖注入的实现思路 如今各个框架都在模块化,连前端的javascript也不例外.每个模块负责一定的功能,模块与模块之间又有相互依赖,那么问题来了:javascript的依赖注入如 ...