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. jQuery小节

    jQuery 语法 jQuery 选择器 在前面的章节中,我们展示了一些有关如何选取 HTML 元素的实例. 关键点是学习 jQuery 选择器是如何准确地选取您希望应用效果的元素. jQuery 元 ...

  2. 使用Goertzel算法识别DTMF信号

    Goertzel算法 Goertzel算法由Gerald Goertzel在1958年提出,用于数字信号处理,是属于离散傅里叶变换的范畴,目的是从给定的采样中求出某一特定频率信号的能量,用于有效性的评 ...

  3. mysql计划字段中有多少个逗号,或者某个标识符

    eg:计划url中有多少个小数点 select length('www.mysql.com')-length(REPLACE('www.mysql.com','.',''));

  4. poj 2104 K-th Number (划分树入门 或者 主席树入门)

    题意:给n个数,m次询问,每次询问L到R中第k小的数是哪个 算法1:划分树 #include<cstdio> #include<cstring> #include<alg ...

  5. POJ 2010 Moo University - Financial Aid treap

    按第一关键字排序后枚举中位数,就变成了判断“左边前K小的和 + 这个中位数 + 右边前K小的和 <= F",其中维护前K小和可以用treap做到. #include <cstdi ...

  6. oracle 密码过期处理

    1.查看用户的proifle是哪个,一般是default sql>SELECT username,PROFILE FROM dba_users; 2.查看指定概要文件(如default)的密码有 ...

  7. 第三次作业——《K米评测》

    第三次作业--<K米评测> 一.调研.评测 上手体验 APP的图标做的不错,一眼就知道和KTV唱歌相关的 点进去就是连接包箱的界面和直播界面,把软件最重要的两个功能展示出来了,一目了然 热 ...

  8. 用python画xy散点图

    import matplotlib.pyplot as plt plt.plot([1,2,3],[4,5,6],'ro') plt.show()#这个智障的编辑器 这样的话,就可以画一个散点图,图中 ...

  9. LINQ - 在Where條件式中使用in與not in

    希望对大家在以后的项目中能用到,我也是在项目中碰到了这个问题: 算算時間,接觸LINQ也有一個月的時間了,可以算是落伍兼新生,不過最近在寫專案的時候,遇到了在LINQ的Where條件式中要如何使用in ...

  10. 解决iis7只能上传30M文件的限制

    首先停止IIS7 服务 访问 下面的目录 X:\Windows\System32\inetsrv\config\schema 用记事本打开 IIS_schema.xml 右键管理员取得权限,以去除只读 ...