python 移动/复制文件

# -*- coding: utf-8 -*-
"""
移动/复制文件的位置
"""
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='D:/1/a.txt'
dstfile='D:/2/a.txt'

mymovefile(srcfile,dstfile)

  

猜你喜欢

转载自www.cnblogs.com/songdongdong6/p/10666007.html