Python按照文件名分类,首先,需要每个文件名具有一定的规律,如下截图中的文件名:
通过Python对上述文件进行分类,按照前面的时间进行分类,并将同一时间的文件保存到同一文件夹下面,文件夹为对应的时间(上图前8位数字)。实现代码如下:
# _*_ coding: utf-8 _*_
__author__ = 'xbr'
__date__ = '2019/3/3 15:58'
import shutil
import os
path = r'F:\FY4A_L1\201804'
files_list = os.listdir(path)
for file in files_list:
filename, suffix = os.path.splitext(file)
date = filename.split('_')[9]
if not os.path.exists(os.path(path, date)):
os.makedirs(os.path(path, date))
shutil.move(os.path(path, file), os.path(path, date))