我制作了一个python脚本,可以从硬盘驱动器中获取所有图像,然后将它们移动到硬盘驱动器上的新文件夹中。这样,可以找到嵌入随机子目录中的所有图像并将其放入此文件夹中。
我有超过830gb的照片需要排序。
然而,在第1486个文件中,程序没有崩溃或冻结,它没有完成,它只是停止。
这是我的代码;
# For each file in the drive
for root, dirs, files in os.walk(drive):
# For each name in the files
for file in files:
if "ImageSorter" in root:
break;
file_name = os.path.splitext(file)[0]
extension = os.path.splitext(file)[1]
# Iterate through each supported file extension
# in our list
if extension in supported_extensions:
# If this is the first file being found
if images_found < 1 and images_moved < 1:
# Init the folder
init_moveto_folder()
images_found += 1
image_dir = os.path.join(root, file);
# Move the file
shutil.move(image_dir, drive + "\\ImageSorter\\" + file_name + "_" + str(images_moved + 1) + extension)
images_moved += 1
if not extension in found_extensions:
found_extensions.append(extension)
print(images_moved)
sleep(0.01)
如果有人知道问题所在,请告诉我。
答案 0 :(得分:0)
我认为问题可能是os.walk
遍历驱动器的方式,以及目标文件夹的交互方式。
这是一个稍微重新组织和简化的代码版本。实际的移动调用已被注释掉,因此您可以先干运行代码,看它是否符合您的要求。希望这有帮助!
init_moveto_folder() # Only check initialization once
for root, dirs, files in os.walk(drive): # For each file in the drive
if 'ImageSorter' in root: # Don't bother with the ImageSorter folder
dirs[:] = [] # Clear out the subdirectory list so `os.walk` doesn't go there
continue
for file in files:
file_name, extension = os.path.splitext(file)
if extension not in supported_extensions:
found_extensions.append(extension)
continue # Extension not supported; continue
images_found += 1
source_path = os.path.join(root, file)
target_path = '%s\\ImageSorter\\%s_%s%s' % (
drive,
file_name,
(images_moved + 1),
extension,
)
print('%s => %s' % (
source_path,
target_path,
))
#shutil.move(source_path, target_path)
images_moved += 1
sleep(0.01)