hi, 我想说应该不会再有问题了.
不知你有试了我寄给你的那个连结的范例了吗? 再贴一次如下, 除了 '.jpg' 也加上 '.png'
# author__ = 'gerry'
# verify if an image is corrupt or not
# help from
https://stackoverflow.com/questions/3964681/find-all-files-in-a-directory-with-extension-txt-in-python
# article found https://www.irishbloke.net/?p=2146
img_dir="Z://rootFolder//imagePath"
corrupt_img_dir="Z://rootFolder//imagePath//corruptImages//"
good_img_dir="Z://rootFolder//imagePath//goodImages//"
from PIL import Image
import os,time
def verify_image(img_file):
#test image
try:
v_image = Image.open(img_file)
v_image.verify()
return True;
#is valid
#print("valid file: "+img_file)
except OSError:
return False;
#main script
for root, dirs, files in os.walk(img_dir):
for file in files:
if file.endswith(".jpg") or file.endswith(".png"):
currentFile=os.path.join(root, file)
#test image
if verify_image(currentFile):
new_file_name=good_img_dir+time.strftime("%Y%m%d%H%M%S_"+os.path.basename(currentFile))
print("good file, moving to dir: "+new_file_name)
try:
os.rename(currentFile, new_file_name)
except WindowsError:
print("error moving file")
else:
#Move to corrupt folder
#makefilename unique
#new_file_name=corrupt_img_dir+time.strftime("%Y%m%d%H%M%S_"+os.path.basename(currentFile))
print("corrupt file")
#os.rename(currentFile, new_file_name)
https://imgur.com/6Da7j0E
※ 引述《s4028600 (佑)》之铭言:
: 简单来说
: 我已经用os.walk列出所有子资料夹的路径
: 然后当作参数进行读取
: 但是读取却只会读取列出来的最后一条
: 我要如何让他读取下一个路径
: 有相关的教学吗
: 网络上只有找到列出路径
: 找不到将这些列出的路径拿来用的方法
: 求详细...