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. Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources解决

    解决方法 ruby -v rvm requirements brew install libyaml rvm pkg install openssl rvm install 2.3.1 --with- ...

  2. NSDate和NSString相互转换

    一.NSDate转NSString //获取系统当前时间 NSDate *currentDate = [NSDate date]; //用于格式化NSDate对象 NSDateFormatter *d ...

  3. python学习之——selenium元素定位

    web自动化测试按步骤拆分,可以分为四步操作:定位元素,操作元素,获取返回结果,断言(返回结果与期望结果是否一致),最后自动出测试报告. 其中定位元素尤为关键,此篇是使用webdriver通过页面各个 ...

  4. js使用Switch达到切换不同颜色的效果

    实现的效果,点击哪个,哪个变颜色,效果如下. 代码如下: <!DOCTYPE html> <html> <head> <meta charset=" ...

  5. NES模拟器开发-CPU笔记

    我的项目XNES已经开始动手编码了,目前的进度大概是cpu的模拟完成了大概10~20%左右.简单记录一下CPU模拟过程中遇到的问题和思考. 原理: cpu模拟实际就是模拟cpu处理opcode的过程, ...

  6. Ubuntu 试用Android L版本

    Android L是最近google一个大更新的版本,目前google开发了android L的开发者预览版本,对于一个android 开发者来说很定是要下载下来体验一把,顺便也要了解一下Androi ...

  7. 使用angularJS遇见的一些问题的解决方案

    1. angularJS的$http.post请求,SpringMVC后台接收不到参数值的解决方案 问题一般为:400 Required String parameter 'rPassword' is ...

  8. Asp.Net MVC4入门指南(7):给电影表和模型添加新字段

    在本节中,您将使用Entity Framework Code First来实现模型类上的操作.从而使得这些操作和变更,可以应用到数据库中. 默认情况下,就像您在之前的教程中所作的那样,使用 Entit ...

  9. 学习总结relative和absolute

    之前对于absolute和relative不了解,现在整理 先设置relative再设置absolute 因为父不设置relative 那么子会向上寻找祖先元素,看是否设置relative.如果有则相 ...

  10. workflow createPath

    针对不同的流程,createpath不同,但是创建审批链,和创建表都有 1.GetUserInfoByListColumn 控件已创建的.先Rebuild,属性需要注意 2.CreateListIte ...