|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码如下:
import os
import time as t
import shutil as stl
class Filesort:
def __init__(self ,path):
self.path = path
self.list_of_path = os.listdir(path)
self.result = {}
self.__checklist() #去掉目录中的文件夹
def __pathlink(self,filename):
return self.path + os.sep + filename
def __checklist(self):
check = []
for each in self.list_of_path:
if os.path.isdir(self.__pathlink(each)):#去掉目录中的文件夹
check.append(each)
for each in check:
self.list_of_path.remove(each)
def __getfiletime(self, filename):
file_info = os.stat(self.__pathlink(filename)) # 合并目录与文件的路径并查看信息
time_of_file = t.localtime(file_info.st_ctime()) # localtime用来转化时间,st_ctime是查看文件创建时间
return time_of_file
def sortfile(self):
print("正在将文件分片")
count = 0
templist = []
for each in self.list_of_file:
templist.append(each)
if count % 20 == 0 or each == self.list_of_path[len(self.list_of_path)-1]:#or 后边是表示文件列表的结束
time_of_file = self.__getfiletime(each)
the_dir_name = str(time_of_file[0:3])
self.result.update({the_dir_name: templist})
templist = []
count += 1
print("有%d个文件即将接受处理。。。" % count)
def makenewdir(self):
print("正在处理文件。。。")
for each in self.result.keys():
os.mkdir(self.__pathlink(each))#创建目录
for eachfile in self.result[each]:#复制文件
stl.copy(self.__pathlink(eachfile),self.__pathlink(each+os.sep+eachfile))
print("处理完成")
path_of_file = 'F:\\手机照片'
sortdemo = Filesort(path_of_file)
sortdemo.sortfile()
sortdemo.makenewdir()
报错是这个
刚开始学python,望指正,谢谢
有list of path没list of file,for循环把电脑搞蒙了
|
|