我有一个坏习惯,从数码相机将文件传输到硬盘的临时文件夹后,会忘记这些文件夹。编程扫描整个硬盘,找到这些遗忘的“照片文件夹”,就太好了。
编写一个程序,遍历硬盘上的每个文件夹,找到可能的照片文件夹。当然,首先你必须定义什么是“照片文件夹”。假定就是超过半数文件是照片的任何文件夹。你如何定义什么文件是照片?
首先,照片文件必须具有文件扩展名.png 或.jpg。此外,照片是很大的图像。照片文件的宽度和高度都必须大于 500 像素。这是安全的假定,因为大多数数码相机照片,宽度和高度都是几千像素。
作为提示,下面是这个程序的粗略框架:
#! python3
# Import modules and write comments to describe this program.
for foldername, subfolders, filenames in os.walk('C:\\'): numPhotoFiles = 0
numNonPhotoFiles = 0
for filename in filenames:
# Check if file extension isn't .png or .jpg. if TODO:
numNonPhotoFiles += 1
continue # skip to next filename # Open image file using Pillow.
# Check if width & height are larger than 500. if TODO:
# Image is large enough to be considered a photo. numPhotoFiles += 1
else:
# Image is too small to be a photo. numNonPhotoFiles += 1
# If more than half of files were photos, # print the absolute path of the folder.
if TODO:
print(TODO)
程序运行时,它应该在屏幕上打印所有照片文件夹的绝对路径。