python 简单可用的复制移动文件

mac2024-03-28  5

用python实现将某代码文件复制/移动到指定路径下。

# -*- coding: utf-8 -*- #!/usr/bin/python import os,shutil def mymovefile(srcfile,dstfile): if not os.path.isfile(srcfile): print "%s not exist!"%(srcfile) else: fpath,fname=os.path.split(dstfile) #分离文件名和路径 if not os.path.exists(fpath): os.makedirs(fpath) #创建路径 shutil.move(srcfile,dstfile) #移动文件 print "move %s -> %s"%( srcfile,dstfile) def mycopyfile(srcfile,dstfile): if not os.path.isfile(srcfile): print "%s not exist!"%(srcfile) else: fpath,fname=os.path.split(dstfile) #分离文件名和路径 if not os.path.exists(fpath): os.makedirs(fpath) #创建路径 shutil.copyfile(srcfile,dstfile) #复制文件 print "copy %s -> %s"%( srcfile,dstfile) srcfile='/root/trainee/cxk1/test.txt' dstfile='/root/trainee/cxk2/test.txt' mymovefile(srcfile,dstfile)
最新回复(0)