[Python] Pitfalls: Be Careful with os.chdir
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的更多相关文章
- 【转载】路径双反斜杠!!!Python IDLE或Python shell中切换路径 切换目录os.chdir("C:\\python37\\2019pythonshel37\\diedai")
Python IDLE或shell中切换路径在Python自带的编辑器IDLE中或者python shell中不能使用cd命令,那么跳到目标路径呢.方法是使用os包下的相关函数实现路径切换功能. im ...
- Python os.chdir() 方法
概述 os.chdir() 方法用于改变当前工作目录到指定的路径. 语法 chdir()方法语法格式如下: os.chdir(path) 参数 path -- 要切换到的新路径. 返回值 如果允许访问 ...
- python os.chdir() 用法
概述 os.chdir() 方法用于改变当前工作目录到指定的路径. 语法 chdir()方法语法格式如下: os.chdir(path) 参数 path -- 要切换到的新路径. 返回值 如果允许访问 ...
- Python读写文件的路径,关于os.chdir(path)位置对程序的影响,
关于os.chdir(path)位置对程序的影响,import os import time#直接把path放到open()里面 def fu0(): star = time.time() for i ...
- python常用模块json、os、sys
一.序列化 json & pickle 模块 json--用于字符串和Python数据类型间进行转换 pickle---用于python特有的类型和Python的数据类型间进行转换 json: ...
- python中模块sys与os的一些常用方法
sys模块提供了访问或操作与python解释器相关方法与对象. 我们就列举出常用到的知识,以后,随着学习,不断补充. 几个常用到的动态对象: sys.argv,这是一个列表,它包含了所有传递给脚本的命 ...
- Python(正则 Time datatime os sys random json pickle模块)
正则表达式: import re #导入模块名 p = re.compile(-]代表匹配0至9的任意一个数字, 所以这里的意思是对传进来的字符串进行匹配,如果这个字符串的开头第一个字符是数字,就代表 ...
- Python:time模块/random模块/os模块/sys模块
time 模块 #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time() 获取当前时间戳 python中时间日期格式化符号: %y 两位数的 ...
- Python函数参数&time、OS、json模块
##可变参数 PORT = 3306 #常量 def mysql(host,user,password,port,charset,sql,db): print('连接mysql') # mysql(' ...
随机推荐
- StringBuilder类型
一.概念 String对象时不可变的,每次使用String类中的一个方法时,都要在内存中创建一个新的字符串对象,这就需要为该对象分配新的空间.在需要对字符串执行重复修改的情况下,与创建新的String ...
- .net core 安装失败 的问题彻底解决
解决方法: 已经整理好包: https://pan.baidu.com/s/1dFuU80p 下载解压运行: DotNetCore.1.0.1-VS2015Tools.Preview2.0.2.e ...
- C# 获取本机CPU序列号,MAC地址,硬盘ID,本机IP地址,计算机名,物理内存,PC类型
首先引入服务 然后 调用 本文转载自http://blog.sina.com.cn/s/blog_7eeb43210101hf7f.html public class Computer { publi ...
- Finereport集群配置
增加配置文件cluster.xml 将配置包resource文件夹下的cluster.xml打开,如果没有cluster.xml,则新建一个,基本内容如下: <?xml version=&quo ...
- mysql远程连接命令(转)
一.MySQL 连接本地数据库,用户名为"root",密码"123"(注意:"-p"和"123" 之间不能有空格) C: ...
- oracle exp imp 导入 正在跳过表 plsql 导入表 成功终止 数据 被导入
http://blog.csdn.net/agileclipse/article/details/12968011 .导入过程中,所有表导入都出现提示, 正在跳过表...某某表名 最后提示成功终止导入 ...
- C/C++入门基础----指针(1)
指针其实就是一个变量, 和其他类型的变量一样.在32位计算机上, 指针占用四字节的变量.指针与其他变量的不同就在于它的值是一个内存地址,指向内存的另外一个地方, 指针能够直接访问内存和操作底层的数据, ...
- Java内存分配及变量存储位置实例讲解
Java内存分配与管理是Java的核心技术之一,之前我们曾介绍过Java的内存管理与内存泄露以及Java垃圾回收方面的知识,今天我们再次深入Java核心,详细介绍一下Java在内存分配方面的知识.一般 ...
- js获取get方式传递的参数
String.prototype.GetValue= function(parm) { var reg = new RegExp("(^|&)"+ parm +" ...
- 脚本录制(JMeter)
使用JMeter录制脚本: 1.打开JMeter工具,创建一个线程组,接着创建一个http代理服务器,代理服务器设置如下: