Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

您现在的位置:首页 > 技术文档 > python入门

通过python将大量文件按修改时间分类的方法

来源:中文源码网    浏览:429 次    日期:2024-04-21 11:01:37
【下载文档:  通过python将大量文件按修改时间分类的方法.txt 】


通过python将大量文件按修改时间分类的方法
需求是这样的,我从本科到现在硬盘里存了好多照片,本来是按类别分的,有一天,我突然想,要是能按照时间来分类可能会更好。可以右键查看照片的属性,看它的修改日期,从而分类,但是十几个G的照片手动分类工作量还是很大的,所以想着写个脚本程序来完成这一个工作。
程序主要是获取文件的修改时间,包括年和月,并以此为名创建文件夹,再用递归调用的方式遍历整个文件夹,将每一张照片拷贝到相应的文件夹下。
程序源码如下:
#coding:utf-8
import os
import sys
import os.path
import time
from shutil import Error
from shutil import copystat
from shutil import copy2
path_str = r"D:\pic";
def copy_file(src_file, dst_dir):
if os.path.isdir(dst_dir):
pass;
else:
os.makedirs(dst_dir);
print(src_file);
print(dst_dir);
copy2(src_file, dst_dir)
def walk_file(file_path):
for root, dirs, files in os.walk(file_path, topdown=False):
for name in files:
com_name = os.path.join(root, name);
t=os.stat(com_name);
copy_path_str = path_str+r"\year"+str(time.localtime(t.st_mtime).tm_year)+r"\month"+str(time.localtime(t.st_mtime).tm_mon);
print(copy_path_str);
copy_file(com_name,copy_path_str);
for name in dirs:
walk_file(name);
walk_file(path_str);
以上这篇通过python将大量文件按修改时间分类的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持中文源码网。

相关内容