我试图整理我的Photos文件夹,我认为我使用Python做到了。我要做的是浏览我的文件夹,检查每张照片(或视频)的扩展名,然后根据其创建日期将其移动到新位置。我从YouTube的视频中得到了这个主意。我还使用了this,现在以下代码一次完美地适用于一个文件夹:
from stat import S_ISREG,ST_CTIME,ST_MODE
import os,sys,time,shutil
dir_path = "F:/Yannis/TestsortingFrom"
info_dict = {}
file_type_dict = {
'CR2' : 'CR2','CR3' : 'CR3','dng' : 'DNG','jpg' : 'JPG','jpeg' : 'JPG','JPG' : 'JPG','bmp' : 'IMAGES','png' : 'IMAGES','ico' : 'IMAGES','MP4' : 'Video','mp4' : 'Video','avi' : 'Video','mov' : 'Video',}
destination_path = "F:/Yannis/SortingDestination/"
year_exists = False
month_exists = False
data = (os.path.join(dir_path,fn) for fn in os.listdir(dir_path))
data = ((os.stat(path),path) for path in data)
data = ((stat[ST_CTIME],path)
for stat,path in data if S_ISREG(stat[ST_MODE]))
for cdate,path in sorted(data):
info_dict.update({os.path.basename(path): time.ctime(cdate) })
for key,value in info_dict.items():
file_date = value.split()
file_extension = key.split(".")
for key2,value2 in file_type_dict.items():
if file_extension[1] == key2:
destination_folder = destination_path +value2
for folder_name in os.listdir(destination_folder):
if folder_name == file_date[4] :
destination_folder1 = destination_folder +'/' + folder_name
year_exists = True
for folder_month in os.listdir(destination_folder1):
if folder_month == file_date[1]:
destination_folder2 = destination_folder1 + '/' + folder_month
month_exists = True
FROM = dir_path + '/' + key
TO = destination_folder2 + '/' + key
shutil.move(FROM,TO)
if not year_exists:
os.mkdir(destination_path + value2 + '/' + file_date[4])
destination_folder1 = destination_path + '/' + value2 + '/' + file_date[4]
if not month_exists:
os.mkdir(destination_path + value2 + '/' + file_date[4] + '/' + file_date[1])
destination_folder2= destination_path + value2 + '/' + file_date[4] + '/' +
file_date[1]
FROM = dir_path + '/' + key
TO = destination_folder2 + '/' + key
shutil.move(FROM,TO)
destination_folder2 = destination_path + '/' + 'Uncategorized'
FROM = dir_path + '/' + key
TO = destination_folder2 + '/' + key
shutil.move(FROM,TO)
我尝试了以下方法来遍历文件夹树,但是它返回FileNotFound错误。
for dirname,dirnames,filenames in os.walk('F:/Yannis/TestsortingFrom'):
for subdirname in dirnames:
dirpath = os.path.join(dirname,subdirname)
我用dirpath替换了先前代码中的dir_path
您有什么想法吗?因为我茫然。我可以一次只处理一个文件,但我想简单地指定一个文件夹并完成它可能很棒。
错误消息如下:
Traceback (most recent call last): File "C:\Users\Yannis\AppData\Local\Programs\Python\Python37\lib\shutil.py",line 566,in move os.rename(src,real_dst) FileNotFoundError: [WinError 2] The system cannot find the file specified: 'F:/Yannis/TestsortingFrom\\Camera/IMG_20110305_143031.jpg' -> 'F:/Yannis/SortingDestination//Uncategorized/IMG_20110305_143031.jpg'
在处理上述异常期间,发生了另一个异常:
Traceback (most recent call last): File "G:/Python/PyCharmProjects/FileSorting/test.py",line 68,in <module> shutil.move(FROM,TO) File "C:\Users\Yannis\AppData\Local\Programs\Python\Python37\lib\shutil.py",line 580,in move copy_function(src,real_dst) File "C:\Users\Yannis\AppData\Local\Programs\Python\Python37\lib\shutil.py",line 266,in copy2 copyfile(src,dst,follow_symlinks=follow_symlinks) File "C:\Users\Yannis\AppData\Local\Programs\Python\Python37\lib\shutil.py",line 120,in copyfile with open(src,'rb') as fsrc: FileNotFoundError: [Errno 2] No such file or directory: 'F:/Yannis/TestsortingFrom\\Camera/IMG_20110305_143031.jpg'
编辑: 现在,以下代码可以正常工作。奇怪的是,如果我的origin_path包含子目录,则程序会将文件从第一个子目录中移出,而其他文件则保持完整。如果我第二次运行它,它将文件从第二个子目录中移出,依此类推。 另一个奇怪的行为是,如果不同类型的文件(在我的情况下为.jpg,.JPG,.dng,.mp4)位于同一子目录中,则程序将移动除.mp4以外的所有文件。如果我第二次运行它,它也会移动.mp4。修改后的代码是:
from pathlib import Path
import os,shutil
from stat import S_ISREG,ST_MODE
class fileToMove():
def __init__(self,name,folder,date):
self.name = name
self.folder = folder
self.date = date
info_list = []
info_dict = {}
file_type_dict = dict(CR2 = 'CR2',CR3 = 'CR3',dng = 'DNG',jpg = 'JPG',jpeg = 'JPG',JPG = 'JPG',bmp = 'IMAGES',png = 'IMAGES',ico = 'IMAGES',mp4 = 'Video',MP4 = 'Video',avi = 'Video',mov = 'Video')
destination_path = Path("F:/Yannis/SortingDestination/")
origin_path = Path('F:/Yannis/TestsortingFrom/')
year_exists = False
month_exists = False
for dirname,filenames in os.walk(origin_path):
for subdirname in dirnames:
dirpath = os.path.join(dirname,subdirname)
data = (os.path.join(dirpath,fn) for fn in os.listdir(dirpath))
data = ((os.stat(path),path) for path in data)
data = ((stat[ST_CTIME],path) for stat,path in data if S_ISREG(stat[ST_MODE]))
for cdate,path in sorted(data):
fl = fileToMove(os.path.basename(path),path,time.ctime(cdate))
info_list.append(fl)
for filez in info_list:
file_date = str(filez.date).split()
file_name = str(filez.name).split('.')
file_folder = str(filez.folder)
print(filez.date,filez.folder,filez.name,file_name)
for key,value in file_type_dict.items():
if key == file_name[1]:
destination_folder = destination_path / value
for folder_name in os.listdir(destination_folder):
if folder_name == file_date[4]:
destination_folder1 = destination_folder / folder_name
year_exists = True
for folder_month in os.listdir(destination_folder1):
if folder_month == file_date[1]:
month_exists = True
FROM = file_folder
TO = destination_folder1 / folder_month / filez.name
shutil.move(FROM,TO)
if not year_exists:
os.mkdir(destination_path / value / file_date[4])
if not month_exists:
os.mkdir(destination_path / value / file_date[4] / file_date[1])
destination_folder2 = destination_path / value / file_date[4] / file_date[1]
FROM = file_folder
TO = destination_folder2 / filez.name
shutil.move(FROM,TO)