需求:根据txt文件中所列文件名及路径,将这些文件复制到另外一个文件夹下并重命名,另外,重新生成一份已复制文件的文件路径及文件名列表。
txt文件名:imglist.txt
原文件所在文件夹:789
目标文件夹:000
新txt文件名:newlist.txt
实现代码:
# -*- coding: utf-8 -*-
import time
import os
import shutil
def re_mycopyfile(srcfile,dstfile,num):
name_long=6
l=len(str(num))
zero='00000000'
if not os.path.isfile(srcfile):
print "%s not exist!"%(srcfile)
else:
#fpath,fname=os.path.split(dstfile) #分离文件名和路径
if not os.path.exists(dstfile):
os.makedirs(dstfile) #创建路径
dstfile=dstfile+zero[:name_long-l-1]+str(num)+'.jpg'
print dstfile
shutil.copyfile(srcfile,dstfile) #复制文件
print "copy %s -> %s"%( srcfile,dstfile)
if __name__ == '__main__':
path1="/Users/sun