通过Python完成在实际工作中关于文件目录操作的4种特定需求
前言
对于工作时上级安排给你的繁复琐碎的重复性工作,为何不让机器去做本该是机器来做的事情。
人生苦短,你用Python。
另,此程序无需修改源码,任意一台电脑上只要搭建环境,双击程序及可运行。
环境
Python3
具体需求
1. 将文件夹中特定格式文件提取到指定文件夹
不论文件夹结构有多复杂,此程序适用于所有文件夹结构。
且此程序通过复制方式提取文件,而非移动。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import os, shutildef copy_file (path1, path2, key_word ): num = 1 for foldName, subfolders, filenames in os.walk(path1): for filename in filenames: if filename.endswith(key_word): new_name = filename.replace(filename, "%i" +"_" +filename)%num shutil.copyfile(os.path.join(foldName, filename), os.path.join(path2, new_name)) print(filename, "copy as" , new_name) num += 1 if __name__ == '__main__' : path1 = input ("请输入你想复制的文件夹的绝对路径:" ) path2 = input ("请输入你想复制进的文件夹的绝对路径:" ) key_word = input ("请输入你想复制文件的后缀名:" ) copy_file(path1, path2, key_word)
2. 将文件夹特定格式文件按原文件夹目录格式提取到指定文件夹
此程序适用于所有文件夹结构。
且此程序通过重新写入文件方式提取文件,而非复制 / 移动。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 import osdef get_file_path (path1, list2, list4 ): list_1 = os.listdir(path1) for i in range (len (list_1)): list_1[i] = ph.join(path1, list_1[i]) if ph.isfile(list_1[i]): list2.append(list_1[i]) else : list4.append(list_1[i]) get_file_path(list_1[i], list2, list4) def make_directories (list4, path2 ): list4.sort(key=lambda x:x.count('\\' )) files_name = list4[0 ] files_name = files_name[files_name.rfind('\\' )+1 :] path2 = path2 + '\\' + files_name for i in range (len (list4)): str_1 = list4[i] str_2 = path2 + str_1[str_1.find(files_name)+len (files_name):] list4[i] = str_2 os.mkdir(list4[i]) def copy_files (list4, list2, path2, key_word ): files_name = list4[0 ] files_name = files_name[files_name.rfind('\\' ) + 1 :] path2 = path2 + '\\' + files_name list2.sort(key=lambda x:x.count('\\' )) for i in range (len (list2)): if key_word == ph.splitext(list2[i])[1 ]: str_1 = list2[i] str_2 = path2 + str_1[str_1.find(files_name)+len (files_name):] print('正在复制文件%s到%s当中' %(list2[i], str_2)) with open (list2[i], 'rb' ) as f: content = f.read() with open (str_2, 'wb' ) as f: f.write(content) if __name__ == '__main__' : list2 = []; list4 = [] path1 = input ('请输入你想复制的文件夹的绝对路径:' ) list4.append(path1) get_file_path(path1, list2, list4) path2 = input ('请输入你想复制进的文件夹的绝对路径:' ) make_directories(list4, path2) key_word = input ('请输入你想复制文件的后缀名:' ) copy_files(list4, list2, path2, key_word)
3. 将文件夹中所有文件提取到指定文件夹
此程序适用于所有文件夹结构。
且此程序通过复制方式提取文件,而非移动。
1 2 3 4 5 6 7 8 9 10 11 12 13 import os, shutildef copy_file (path1, path2 ): for foldName, subfolders, filenames in os.walk(path1): for filename in filenames: shutil.copyfile(os.path.join(foldName, filename), os.path.join(path2, filename)) print(filename, "已复制完成" ) if __name__ == '__main__' : path1 = input ("请输入你想复制的文件夹的绝对路径:" ) path2 = input ("请输入你想复制进的文件夹的绝对路径:" ) copy_file(path1, path2)
4. 提取文件夹中文件创建(修改)日期至Excel表
此程序只适用于一层文件夹包含所有指定文件的文件夹结构。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import os, timeimport pandas as pddef get_time (path1, path2, excelName ): filenames = [] cTimes = [] mTimes = [] files = os.listdir(path1) for file in files: fp = os.path.join(path1, file) createdTime = time.localtime(os.stat(fp).st_ctime) modifiedTime = time.localtime(os.stat(fp).st_mtime) cTime = time.strftime('%Y-%m-%d %H:%M:%S' , createdTime) mTime = time.strftime('%Y-%m-%d %H:%M:%S' , modifiedTime) filenames.append(file) cTimes.append(cTime) mTimes.append(mTime) d = {"文件名称" :filenames, "创建时间" :cTimes, "修改时间" :mTimes} df = pd.DataFrame(d) df.to_excel(path2 + "\\" + excelName) if __name__ == '__main__' : path1 = input ("请输入文件所在的文件夹的绝对路径:" ) path2 = input ("请输入文件创建时间提取后的Excel表所在的文件夹的绝对路径:" ) excelName = input ("请输入想创建的Excel名称(后缀名也要写,如.xlsx):" ) get_time(path1, path2, excelName) print("提取完成!" )