用python實現將某代碼文件復制/移動到指定路徑下。
場景例如:mv ./xxx/git/project1/test.sh ./xxx/tmp/tmp/1/test.sh (相對路徑./xxx/tmp/tmp/1/不一定存在)
# -*- coding: utf-8 -*-
#!/usr/bin/python
#test_copyfile.py
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='/Users/xxx/git/project1/test.sh'
dstfile='/Users/xxx/tmp/tmp/1/test.sh'
mymovefile(srcfile,dstfile)