遍历文件夹和子文件夹中的所有文件并获取创建日期

Iterate over all files in folder and subfolders and get creation date

我正在尝试整理我的照片文件夹,我想我是用 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)

我尝试了以下方法来遍历文件夹树,但它 returns 出现 FileNotFound 错误。

for dirname, dirnames, filenames in os.walk('F:/Yannis/TestSortingFrom'):
        for subdirname in dirnames:
            dirpath = os.path.join(dirname, subdirname)

我把之前代码中的dir_path替换成了dirpath

你有什么想法吗?因为我不知所措。我可以一次处理一个文件,但我认为简单地指定一个文件夹并完成它可能会很棒。

错误信息如下:

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, time, shutil
from stat import S_ISREG, ST_CTIME, 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, dirnames, 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)
  1. 错误文件name/path.
  2. 您应该将路径转换为原始字符串,如下所示:

for dirname, dirnames, filenames in os.walk(r'F:/Yannis/TestSortingFrom'): for subdirname in dirnames: dirpath = os.path.join(dirname, subdirname)

from pathlib import Path
import os, time, shutil
from stat import S_ISREG, ST_CTIME, 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/Photos/2019/9-2-2019/')

for dirname, dirnames, 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)
            print(fl.date)
for filez in info_list:
    file_date = str(filez.date).split()
    file_name = str(filez.name).split('.')
    file_folder = str(filez.folder)
    if file_name[1] not in file_type_dict:
        destination_folder3 = destination_path / 'Uncategorized'
        FROM = file_folder
        TO = destination_folder3 / filez.name
        shutil.move(FROM, TO)
    else:
        for key, value in file_type_dict.items():
            if key == file_name[1]:
                year_exists = False
                month_exists = False
                day_exists = False
                destination_folder = destination_path / value
                for folder_year in os.listdir(destination_folder):
                    if folder_year == file_date[4]:
                        destination_folder_year = destination_folder / folder_year
                        year_exists = True
                        for folder_month in os.listdir(destination_folder_year):
                            if folder_month == file_date[1]:
                                destination_folder_month = destination_folder_year / folder_month
                                month_exists = True
                                for folder_day in os.listdir(destination_folder_month):
                                    if folder_day == file_date[2]:
                                        day_exists = True
                                        FROM = file_folder
                                        TO = destination_folder_year / folder_month / folder_day / 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])
                if not day_exists:
                    os.mkdir(destination_path / value / file_date[4] / file_date[1] / file_date[2])
                    destination_folder2 = destination_path / value / file_date[4] / file_date[1] /file_date[2]
                    FROM = file_folder
                    TO = destination_folder2 / filez.name
                    shutil.move(FROM, TO)