脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 通过python将大量文件按修改时间分类的方法

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

2021-04-08 00:37AlexAcce Python

今天小编就为大家分享一篇通过python将大量文件按修改时间分类的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

需求是这样的,我从本科到现在硬盘里存了好多照片,本来是按类别分的,有一天,我突然想,要是能按照时间来分类可能会更好。可以右键查看照片的属性,看它的修改日期,从而分类,但是十几个G的照片手动分类工作量还是很大的,所以想着写个脚本程序来完成这一个工作。

程序主要是获取文件的修改时间,包括年和月,并以此为名创建文件夹,再用递归调用的方式遍历整个文件夹,将每一张照片拷贝到相应的文件夹下。

程序源码如下:

?
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
#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将大量文件按修改时间分类的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/Alex1syyl/article/details/53997054

延伸 · 阅读

精彩推荐