主要用来做数据备份,每次用完以后再跑一次脚本,又可以将文件夹下的所有文件拷贝到指定的文件夹内
import os,sys,shutil; class cur_env: path = sys.path[0] #print(path) os.chdir(path) # copy files to multiple folders class copyfiles: def __init__(self, psrc, pdst): self._src_path = psrc self._dst_path = pdst self._get_emails() self._get_folders() # get the log file list in the folder def _get_emails(self): files = os.listdir(self._src_path) self._file_list = [x for x in files if x.endswith('.log')] # get email account folders def _get_folders(self): temp = os.listdir(self._dst_path) self._folders = [x for x in temp if os.path.isdir(x)] # copy files to folders def copyfiles_to_folders(self): for folder in self._folders: for file in self._file_list: shutil.copy(self._src_path + '\\' + file,\ self._dst_path + '\\' + folder) # del files in dst folders def del_dst_files(self): for folder in self._folders: files = os.listdir(self._dst_path + '\\' + folder) for file in files: os.remove(self._dst_path + '\\'+folder+'\\'+file) temp = copyfiles('C:\\Users\\sunyu\\Desktop\\emails',cur_env.path)
我把数据备份在桌面的email文件夹内。有两个方法,一个是将文件全部拷贝到指定根目录下所有的文件夹内,另外一个是将指定根目录下所有文件夹内的文件清空。