复制文件和删除文件

 
import os
import shutil


def
CopyFile(original_path,target_path,choose_item): # get all file path under the given folder path_list = [] list1 = os.listdir(original_path) # 列出文件夹下所有的目录与文件 for i in range(0, len(list1)): com_path = os.path.join(original_path, list1[i]) target_path1 = os.path.join(target_path,list1[i]) if os.path.isdir(com_path): if not os.path.isdir(target_path1): os.makedirs(target_path1) #print(target_path1) CopyFile(com_path,target_path1,choose_item) if os.path.isfile(com_path): for item in choose_item: if com_path.find(item) > 0: fname = os.path.join(target_path1.replace('\\','"',5)[:target_path1.replace('\\','"',5).find('\\')].replace('"','\\'), os.path.split(target_path1)[1]) #希望保存在目标目录的子文件目录下,而不是按照原有文件结构进行复制 shutil.copyfile(com_path,fname) def remove_file(target_path): list1 = os.listdir(target_path) # 列出文件夹下所有的目录与文件 for i in range(0, len(list1)): target_path1 = os.path.join(target_path, list1[i]) # print(com_path) if os.path.isdir(target_path1): if len(os.listdir(target_path1)) == 0: os.rmdir(target_path1) else: remove_img(target_path1)

猜你喜欢

转载自www.cnblogs.com/learning-logs/p/12916158.html