序言 (https://jq.qq.com/?_wv=1027&k=GmeRhIX0)

当我们电脑里面的文本或者或者文件夹太多了,有时候想找到自己想要的文件,只能通过去搜索文件名,要是名字忘记了的话,那你也搜不了吧,当然你可通过后缀名去搜索,但是通过搜索出来的文件只会更多,这还只是文件多的情况下。

如果文件名字基本一样,只有序号不一样呢?那特么眼睛看瞎了估计还没找到。

那么我们可以用Python来对文件进行批量自动分类保存到文件夹,下次找的时候,就很轻松的能够找到了。

我们这里以这两百多个world原始文件为例

代码展示

python学习资料交流群:660193417###
import os
import time
import datetime
import pprint
import shutil
import re origin_dir = '原始文件/'
target_dir = '目标文件/'
files = os.listdir(origin_dir) python学习资料交流群:660193417###
month_compile = re.compile('(20\d\d)([01][0-9])')
print(files)
file_dict = {}
for file in files:
if file.endswith('.docx'):
word_file = os.path.join(origin_dir, file)
month = month_compile.search(word_file)
if month:
month = month.groups()
# print(month.groups())
if month[-1] == '00':
month = re.search('(20\d\d)0([01][0-9])', word_file).groups()
t1 = '-'.join(month)
if t1 in file_dict.keys():
file_dict[t1].append(word_file)
else:
file_dict[t1] = [word_file] pprint.pprint(file_dict) if not os.path.exists(target_dir):
os.mkdir(target_dir)
# 移动文件
for key in file_dict.keys():
# 创建文件的月份
new_file_dir = os.path.join(target_dir, key)
if not os.path.exists(new_file_dir):
os.mkdir(new_file_dir)
for file in file_dict[key]:
# 获取文件名
new_file_name = os.path.basename(file)
# 合并新的文件路径
new_file_path = os.path.join(new_file_dir, new_file_name)
print(new_file_name)
shutil.copyfile(file, new_file_path)

效果展示

我们这里以按月份分类为例,自动创建文件夹进行分类。

看,这不就好找多了

兄弟们,快去试试吧!

最新文章

  1. Python学习day3作业
  2. JS判断字符串长度的5个方法
  3. weui 问题
  4. my_strcpy()
  5. 【第一课】神奇的Context
  6. acdream Divide Sum
  7. ASIHTTPRequest详解
  8. Java Bean 获取properties文件的读取
  9. JS获取按下的键盘字符
  10. C#之网络
  11. CentOS-6.5x64:VNC安装配置
  12. bootstrap table分页后刷新跳到第一页
  13. GCC编译选项 -OX[转]
  14. postman使用详解
  15. parse
  16. 关于input
  17. 感知机、logistic回归 损失函数对比探讨
  18. JavaScript:谈谈let和const
  19. 终端:Xcode模拟器上安装.app方法
  20. 德哥PostgreSQL学习资料汇总(转)

热门文章

  1. 【GPLT】 2018年天梯赛全国总决赛 L2-2 小字辈(c++)
  2. Revit二次开发之添加选项卡和按钮
  3. Java中时间类中的Data类与Time类
  4. Python抽象基类:ABC谢谢你,因为有你,温暖了四季!
  5. 数据结构篇(2) ts实现单循环链表
  6. Git批量下载MODIS数据
  7. socket编程实现tcp服务器_C/C++
  8. 【必看】局域网IP地址冲突罪魁祸首是谁?
  9. 20220303模拟赛题解and总结
  10. 从NSSRound#1学到了什么