python shutil 模块 的剪切文件函数 shutil.movemove(src, dst),换用 os.rename(sourceFile, targetFile)
Google 一搜python 剪切文件,出来shutil 这模块,网上很多人也跟疯说shutil.move(src, dst)就是用来剪切文件的,结果一试,剪切毛线,文件都复制到另一个文件夹了,源文件还在,因为我的源文件正在被另一个程序使用,所以shutil.move(src, dst)把源文件复制到别的地方后没法再对源文件进行删除,这冒牌货却仍保留着复制后的文件。美其名曰移动文件。。。。网上也有人给出了shutil.move(src, dst)的源码,先来看下它的源码吧。。。
def move(src, dst):
real_dst = dst
if os.path.isdir(dst):
real_dst = os.path.join(dst, _basename(src))
if os.path.exists(real_dst):
raise Error, "Destination path '%s' already exists" % real_dst
try:
os.rename(src, real_dst)
except OSError:
if os.path.isdir(src):
if destinsrc(src, dst):
raise Error, "Cannot move a directory '%s' into itself '%s'." % (src, dst)
copytree(src, real_dst, symlinks=True)
rmtree(src)
else:
copy2(src, real_dst)
os.unlink(src)
很明显,函数没有对当前正在被别的程序使用的文件进行判断,简单的先复制再删除,删除不了的也就不管了。分析了此源码的人到最后却还在说此功能类似于windows的ctrl+x->ctrl+v操作。。你是傻吗。。。
本来想自己写个程序判断的,但是懒得写了,最后发现用os.rename(sourceFile, targetFile)可以完美解决。。。就用它了。。。
python shutil 模块 的剪切文件函数 shutil.movemove(src, dst),换用 os.rename(sourceFile, targetFile)的更多相关文章
- shutil模块——高级的文件、文件夹、压缩包处理模块
将文件内容拷贝到另一个文件 shutil.copyfileobj('fsrc', 'fdst', 'length') 方法源码: def copyfileobj(fsrc, fdst, length= ...
- shutil 模块 高级的文件、文件夹、压缩包 处理模块
高级的文件.文件夹.压缩包 处理模块 # 将文件内容拷贝到另一个文件中 shutil.copyfileobj(fsrc, fdst[, length]) import shutil shutil.co ...
- [python]glob模块中的glob()函数为什么返回空列表??
最近在学习语音的知识,看一个语音合成实现的相关工具包的源代码,碰到了glob()函数.然后开启了我与这个函数相爱想杀的一个下午. 摘自官网解释: https://docs.python.org/2/l ...
- python xlwt模块生成excel文件并写入数据 xlrd读取数据
python中一般使用 xlwt (excel write)来生成Excel文件(可以控制单元格格式),用 xlrd 来读取Excel文件,用xlrd读取excel是不能对其进行操作的. 1.xlrd ...
- Python logging模块 控制台、文件输出
步骤 导入logging模块 设置level(此处是DEBUG) 添加文件handler和流handler import logging logger=logging.getLogger(__name ...
- Python中模块、类、函数、实例调用案例
19 a = '我是模块中的变量a' 20 21 def hi(): 22 a = '我是函数里的变量a' 23 print('函数"hi"已经运行!') 24 25 class ...
- Python itertools模块中的product函数
product 用于求多个可迭代对象的笛卡尔积(Cartesian Product),它跟嵌套的 for 循环等价.即: product(A, B) 和 ((x,y) for x in A for y ...
- Web 在线文件管理器学习笔记与总结(15)剪切文件夹 (16)删除文件夹
(15)剪切文件夹 ① 通过rename($oldname,$newname) 函数实现剪切文件夹的操作 ② 需要检测目标文件夹是否存在,如果存在还要检测目标目录中是否存在同名文件夹,如果不存在则剪切 ...
- [转]python中对文件、文件夹的操作——os模块和shutil模块常用说明
转至:http://l90z11.blog.163.com/blog/static/187389042201312153318389/ python中对文件.文件夹的操作需要涉及到os模块和shuti ...
随机推荐
- java类型与Hadoop类型之间的转换
java基本类型与Hadoop常见基本类型的对照Long LongWritableInteger IntWritableBoolean BooleanWritable String Text ...
- Calling convention-调用约定
In computer science, a calling convention is an implementation-level (low-level) scheme for how subr ...
- h5实现 微信的授权登录
本文重点 判断是不是微信环境 localstorage设置一个值 微信授权登录 获取一个时间戳 new Date().getTime() const wx = (function () { retur ...
- 2017年6月28日 python爬虫学习
1.写入csv文件2.lxml的用法3.自定义字典类的方法4.bytes解码得到str,str编码得到bytes5.json 1 import csv import lxml.html class S ...
- HDU 1796 How many integers can you find(容斥原理)
题意 就是给出一个整数n,一个具有m个元素的数组,求出1-n中有多少个数至少能整除m数组中的一个数 (1<=n<=10^18.m<=20) 题解 这题是容斥原理基本模型. 枚举n中有 ...
- linux 杀掉端口
netstat -apn|grep 8184 tcp 0 0 0.0.0.0:8184 0.0.0.0:* LISTEN ...
- Maven搭建之后的设置
Maven搭建之后的设置 1,设置环境变量M2_HOME=D:\Java\apache-maven-3.3.9, MAVEN_OPTS=-Xms128m -Xmx512m 在path中,添加D:\Ja ...
- [MST] Store Store in Local Storage
For an optimal user and developer experience, storing state in local storage is often a must. In thi ...
- drupal7 怎样将一个date字段加入上日期插件效果
//这里以created字段为样例 function Hook_form_alter($form,$form_state,$form_id){ $form['created']['#type'] = ...
- JavaScript检查手机格式是否错误
编写自己定义的JavaScript函数checkPhone(),在函数中应用正則表達式推断手机号码的格式是否正确,不对的给出提示 <script type="text/javascri ...