作者alvinlin (艾尔文)
看板Python
标题Re: [问题] 读取下一个资料夹路径
时间Fri Aug 2 22:52:46 2019
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列出所有子资料夹的路径
: 然後当作参数进行读取
: 但是读取却只会读取列出来的最後一条
: 我要如何让他读取下一个路径
: 有相关的教学吗
: 网路上只有找到列出路径
: 找不到将这些列出的路径拿来用的方法
: 求详细...
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 123.192.186.172 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1564757571.A.634.html
1F:推 s4028600: 你给的那个网址的代码不管图片好坏都归类为错误 08/03 00:16
2F:→ s4028600: 你这个代码直接无法执行... 08/03 00:17
3F:→ alvinlin: 这个代码就是我给你的连结的代码。程式我跑过才给出来的 08/03 02:01
4F:→ alvinlin: 。你对照图确认一下代码输入是对的。 08/03 02:01
5F:→ alvinlin: Python 很重视缩排的。所有的缩排要正确 08/03 02:02
6F:推 s4028600: 呃...原本的代码就已经判断不了图片是否完整了... 08/03 06:49
7F:推 art1: 可能复制bbs的程式码直接贴上去? 这样很容易缩排错误 08/03 23:04
8F:→ alvinlin: 呃...唉 08/04 02:28
9F:→ alvinlin: 这年头当好人真不容易 08/04 02:29
10F:推 s4028600: 不会 因为我根本不会写 一开始找到的代码根本不能用 至 08/04 05:26
11F:→ s4028600: 少不能用在我的图片上 08/04 05:26
12F:→ s4028600: 所以才换代码 只是也瓶颈就是了 08/04 05:27
13F:推 s4028600: 不是缩排问题 他的代码和原本的代码判断方式相同 但是却 08/04 05:31
14F:→ s4028600: 无法判断我的图片 所以只能放弃了 08/04 05:31
15F:推 s4028600: 现在头痛的是当os.walk拿来当str的时候 只能读取最後一 08/04 05:35
16F:→ s4028600: 行 网路上没有找到相似的东西能让我改... 08/04 05:35
17F:→ s4028600: 呃...我应该没搞错str的意思吧... 08/04 05:36