Python例子

Python复制文件到另外一个文件夹

在实际工作中,我们会用到python复制文件到另外一个文件夹。上代码:

import os
import shutil
def copyfile(file_path, back_dir):  # 复制函数
    if not os.path.isfile(file_path):
        print("%s not exist!" % (file_path))
    else:
        fpath, fname = os.path.split(file_path)  # 分离文件名和路径
        if not os.path.exists(back_dir):
            os.makedirs(back_dir)  # 创建路径
        shutil.copy(file_path, back_dir + fname)  # 复制文件
        print("copy %s -> %s" % (file_path, back_dir + fname))

# if __name__ == '__main__':
#     file_path = r'/www/wwwroot/xray_scan/scan_result/https_66_220_31_47.html'
#     back_dir = r'/www/wwwroot/xray_scan/scan_result_back/'
#     copyfile(file_path, back_dir)


最后修改:2021-07-31 17:45:00