前言:
平时工作没有养成分类的习惯,整个桌面杂乱无章都是文档和资料,几乎快占满整个屏幕了。所以必须要整理一下了,今天我们来看下用python如何批量将不同后缀的文件移动到同一文件夹。
演示效果:
- 使用前
- 使用后
代码:
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# # -*- coding:utf-8 -*- import os import glob import shutil import tkinter import tkinter.filedialog from datetime import datetime def start(): root = tkinter.Tk() root.withdraw() dirname = tkinter.filedialog.askdirectory(parent = root,initialdir = "/" ,title = '请选择文件夹' ) return dirname # 定义一个文件字典,不同的文件类型,属于不同的文件夹 file_dict = { "图片" : [ "jpeg" , "jpg" , "tiff" , "gif" , "bmp" , "png" , "bpg" , "svg" , "heif" , "psd" ], "视频" : [ "avi" , "flv" , "wmv" , "mov" , "mp4" , "webm" , "vob" , "mng" , "qt" , "mpg" , "mpeg" , "3gp" , "mkv" ], "音频" : [ "aac" , "aa" , "aac" , "dvf" , "m4a" , "m4b" , "m4p" , "mp3" , "msv" , "ogg" , "oga" , "raw" , "vox" , "wav" , "wma" ], "文档" : [ "oxps" , "epub" , "pages" , "docx" , "doc" , "fdf" , "ods" , "odt" , "pwi" , "xsn" , "xps" , "dotx" , "docm" , "dox" , "rvg" , "rtf" , "rtfd" , "wpd" , "xls" , "xlsx" , "xlsm" , "ppt" , "pptx" , "csv" , "pdf" , "md" , "xmind" ], "压缩文件" : [ "a" , "ar" , "cpio" , "iso" , "tar" , "gz" , "rz" , "7z" , "dmg" , "rar" , "xar" , "zip" ], "文本" : [ "txt" , "in" , "out" , "json" , "xml" , "log" ], "程序脚本" : [ "py" , "html5" , "html" , "htm" , "xhtml" , "cpp" , "java" , "css" , "sql" ], '可执行程序' : [ 'exe' , 'bat' , 'lnk' , 'sys' , 'com' , 'apk' ], '字体文件' : [ 'eot' , 'otf' , 'fon' , 'font' , 'ttf' , 'ttc' , 'woff' , 'woff2' , 'shx' ], '工程图文件' :[ 'bak' , 'dwg' , 'dxf' , 'dwl' , 'dwl2' , 'stp' , 'SLDPRT' , 'ipj' , 'ipt' , 'idw' ] } # 定义一个函数,传入每个文件对应的后缀。判断文件是否存在于字典file_dict中; # 如果存在,返回对应的文件夹名;如果不存在,将该文件夹命名为"未知分类"; def JudgeFile(suffix): for name, type_list in file_dict.items(): if suffix.lower() in type_list: return name return "未知分类" if __name__ = = '__main__' : try : while True : path = start() print ( "---->路径是: " ,path) if path = = "": print ( "没有选择路径!" ) break # 递归获取 "待处理文件路径" 下的所有文件和文件夹。 startTime = datetime.now().second for file in glob.glob(f "{path}/**/*" , recursive = True ): # 由于我们是对文件分类,这里需要挑选出文件来。 if os.path.isfile( file ): # 由于isfile()函数,获取的是每个文件的全路径。这里再调用basename()函数,直接获取文件名; file_name = os.path.basename( file ) suffix = file_name.split( "." )[ - 1 ] # 判断 "文件名" 是否在字典中。 name = JudgeFile(suffix) # 根据每个文件分类,创建各自对应的文件夹。 if not os.path.exists(f "{path}\\{name}" ): os.mkdir(f "{path}\\{name}" ) print ( 'path-->' ,name) # 将文件复制到各自对应的文件夹中。 # shutil.copy(file, f"{path}\\{name}") # 将文件移动到各自对应的文件夹中。 shutil.move( file , f "{path}\\{name}" ) endTime = datetime.now().second countTime = endTime - startTime print ( "---->已经整理完成。共花费 {} s" . format (countTime)) a = input ( '---->请按回车键退出:' ) if a = = '': break except BaseException: print ( '存在重复的文件!' ) |
执行起来很简单,只要写完程序,点击程运行,等待弹出窗口,选择需要整理的文件夹即可。
如果觉得以上代码觉得复杂,可以尝试以下更为简单的程序。
如何实现文件自动分类?
同一目录下存在很多不同类型的资源条件
- 1 .分类
- 2.创建分类目录
- 3.移动文件资源
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
|
import os import shutil import tkinter import tkinter.filedialog from datetime import datetime def start(): root = tkinter.Tk() root.withdraw() dirname = tkinter.filedialog.askdirectory(parent = root,initialdir = "/" ,title = '请选择文件夹' ) return dirname # 源文件存在路径 src_dir = start() # 分类资源存在路径 dest_dir = src_dir # 判断目录是否存在 if not os.path.exists(dest_dir): os.mkdir(dest_dir) # 源目录分析 files = os.listdir(src_dir) for item in files: src_path = os.path.join(src_dir,item) # 判断状态 if os.path.isfile(src_path): #如果是文件,进入代码块 # 判断文件资源的类型 ndir = item.split( '.' )[ - 1 ] desc_path = os.path.join(dest_dir,ndir) # 创建分类目录 if not os.path.exists(desc_path): # 如果分类子目录不存在,创建 os.mkdir(desc_path) shutil.move(src_path,desc_path) |
到此这篇关于python实现自动整理文件的文章就介绍到这了,更多相关python整理文件内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_42750611/article/details/124051648