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. text()和html()的区别,以及val()

    text():设置或返回所选元素的文本内容: html():设置或返回所选元素的内容(包括 HTML 标记): val():设置或返回表单字段的值 例如: <!DOCTYPE html>& ...

  2. MongoDB Replica Set 选举过程

    什么是选举? 选举是副本集选择某个成员成为primary的过程.primary是一个副本集中唯一能够接收写操作的成员. 下面的事件能够引发一次选举: 第一次初始化一个副本集 Primary失效.rep ...

  3. 解决:NoSuchAlgorithmException: Algorithm HmacSHA1 not available

    windows下运行macInstance = Mac.getInstance("HmacSHA1");完全正常,Linux下则出现异常: java.security.NoSuch ...

  4. Memento(备忘录)-对象行为型模式

    1.意图 在布破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可以将对象恢复到原先保存的状态. 2.别名 Token 3.动机 为了允许用户取消不确定的操作或从错误中 ...

  5. 创建并追加img元素(jquery!)

    有几种方法 但都需要你指定一个节点 根据这个节点进行添加 如现有一节点Id为pr:一,向该节点内部后方添加:1 $("#pr").append("<img src= ...

  6. 关于for循环删除数组内容出现的问题

    昨天用for循环进行数组去重的时候出现的问题, 首先,用双重for循环把前一个和所有后面的元素进行比较,如果相等则删除. 但是,如果数组里面有三个以上连续相等的元素的时候,就会出现问题. var ar ...

  7. Servlet目录

    Servlet目录   课时1 Servlet概述12:30 课时2 servlet的第一个例子31:08 课时3 servlet的生命周期18:18 课时4 HttpServlet详细讲解31:43 ...

  8. 十分钟使用github pages +hexo拥有个人博客

    最近想自己搭建自己的个人博客,毕竟这样觉得比较geek,但是搜了资料,感觉良莠不齐,好多东西说的含糊不清,于是自己记录下自己的搭建过程. 1, 安装nodejs 2, 安装git 3, 申请githu ...

  9. Oracle 服务器端执行带参数的procedure

    进入服务器后 su - oracle sqlplus schema/schemapass 连接上以后,输入以下,然后执行 declare vRet number(5) := 8; begin proc ...

  10. 插入排序 - C语言

    插入排序的思想: 以现有的已排序元素为基础,下一个元素添加到正确的位置,则最终会完成排序. 第一个元素本身是已经排序好的.从第二个开始排. void insertSort(int arr[], int ...