Python移动复制文件

直接贴代码 

import os, shutil


def movefile(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 TO %s" % (srcfile, dstfile))


def copyfile(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 TO %s" % (srcfile, dstfile))


sourceFile = './actwork/linkFile/allExtLinks - copy.txt'
destFile = './actwork/allExtLinks - copy.txt'

movefile(sourceFile, destFile)

猜你喜欢

转载自blog.csdn.net/jiahao1186/article/details/89316279