標籤:
主要用來做資料備份,每次用完以後再跑一次指令碼,又可以將檔案夾下的所有檔案拷貝到指定的檔案夾內
import os,sys,shutil;class cur_env: path = sys.path[0] #print(path) os.chdir(path)# copy files to multiple foldersclass 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檔案夾內。有兩個方法,一個是將檔案全部拷貝到指定根目錄下所有的檔案夾內,另外一個是將指定根目錄下所有檔案夾內的檔案清空。
python拷貝檔案到多個檔案夾