python26实例[文件copy和自动rename]
2011-03-22 18:09
537 查看
用来copy文件和目录,当文件或文件夹已经存在时,自动增加.r1,.r2......来重命名新copy的文件。
代码:
import os
import sys
import shutil
def copyWithRename(source, dest, rename = True):
if os.path.exists(dest) and rename == True:
dir, name = os.path.split(dest)
newdest = dest
if os.path.isfile(dest):
namewithoutext, ext = os.path.splitext(name)
i = 1
while(1):
newdest = os.path.join(dir, namewithoutext + '.r' + str(i) + ext)
if os.path.exists(newdest):
i+=1
continue
else: break
else:
i = 1
while(1):
newdest = os.path.join(dir, name + '.r' + str(i))
if os.path.exists(newdest):
i+=1
continue
else: break
dest = newdest
print 'Copied : ' + source + ' >>> ' + dest
if os.path.isdir(source):
shutil.copytree(source,dest)
else:
shutil.copyfile(source,dest)
def usage():
thescript = sys.argv[0]
usage = '\n\
Function:\n\
Copy file or folder and rename it with .rx suffix\n\
when the same file or folder is already existed.\n\
Usage:\n\
python %s source dest\n\
Eexamples:\n\
python %s "c:\\test\\test.txt" "c:\\test2\\test.txt"\n\
python %s "c:\\test\\test1" "c:\\test\\test2"\n\
Notes:\n\
source and dest must be same type, such as both of them are file or dir.\n\
' % (thescript,thescript,thescript)
print usage
if __name__ == '__main__':
if len(sys.argv) == 3:
copyWithRename(sys.argv[1], sys.argv[2])
else:
usage()
完!
代码:
import os
import sys
import shutil
def copyWithRename(source, dest, rename = True):
if os.path.exists(dest) and rename == True:
dir, name = os.path.split(dest)
newdest = dest
if os.path.isfile(dest):
namewithoutext, ext = os.path.splitext(name)
i = 1
while(1):
newdest = os.path.join(dir, namewithoutext + '.r' + str(i) + ext)
if os.path.exists(newdest):
i+=1
continue
else: break
else:
i = 1
while(1):
newdest = os.path.join(dir, name + '.r' + str(i))
if os.path.exists(newdest):
i+=1
continue
else: break
dest = newdest
print 'Copied : ' + source + ' >>> ' + dest
if os.path.isdir(source):
shutil.copytree(source,dest)
else:
shutil.copyfile(source,dest)
def usage():
thescript = sys.argv[0]
usage = '\n\
Function:\n\
Copy file or folder and rename it with .rx suffix\n\
when the same file or folder is already existed.\n\
Usage:\n\
python %s source dest\n\
Eexamples:\n\
python %s "c:\\test\\test.txt" "c:\\test2\\test.txt"\n\
python %s "c:\\test\\test1" "c:\\test\\test2"\n\
Notes:\n\
source and dest must be same type, such as both of them are file or dir.\n\
' % (thescript,thescript,thescript)
print usage
if __name__ == '__main__':
if len(sys.argv) == 3:
copyWithRename(sys.argv[1], sys.argv[2])
else:
usage()
完!
相关文章推荐
- python26实例[文件copy和自动rename]
- Python 文件/文件夹操作(读写read/write、复制copy、删除delete、重命名rename)
- 详解python脚本自动生成需要文件实例代码
- python实例26[查询修改文件的属性]
- python实例26[查询修改文件的属性]
- python实例26[将log同时写入文件和显示到UI的控件中]
- python 实现自动远程登陆scp文件实例代码
- python实例26[将log同时写入文件和显示到UI的控件中]
- Python 文件读写操作实例详解
- PHP实现UTF-8文件BOM自动检测与移除实例
- python中操作文件函数open的简单操作实例
- Python使用difflib对比两个文件操作实例
- python实现的用于搜索文件并进行内容替换的类实例
- Python实例浅谈之六读写文件
- python fabric模块 生产环境dubbo包全自动发布管理完整实例
- 进度条JProgressBar结合线程实现copy文件进度实例
- python文件读写实例,超级方便
- python去除文件中重复的行实例
- python写一段脚本代码自动完成输入(目录下的所有)文件的数据替换(修改数据和替换数据都是输入的)
- Python利用rsync自动同步服务器与本地文件同步