线上有需求,就是根据一个文件列表,去复制到相应的目录,而且要保持目录结构。
贴代码
#!/usr/bin/env python ##coding=utf-8 print """ 从存储服务器导出到临时目录 """ import os import shutil from os.path import exists, abspath, dirname, isfile des_dir = "/data" f = open('file.list', 'r') file = f.readlines() for i in file: if isfile(i.rstrip()): src_file_path = abspath(i.rstrip()) src_dir_path = dirname(src_file_path) des_dir_full = des_dir + src_dir_path des_file_full = des_dir + src_file_path if not exists(des_dir_full): os.makedirs(des_dir_full) if not exists(des_file_full) and exists(src_file_path): shutil.copy(src_file_path, des_dir_full)