Re: [問題] 讀取下一個資料夾路徑
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://www.ptt.cc/bbs/Python/M.1564757571.A.634.html
推
08/03 00:16,
6年前
, 1F
08/03 00:16, 1F
→
08/03 00:17,
6年前
, 2F
08/03 00:17, 2F
→
08/03 02:01,
6年前
, 3F
08/03 02:01, 3F
→
08/03 02:01,
6年前
, 4F
08/03 02:01, 4F
→
08/03 02:02,
6年前
, 5F
08/03 02:02, 5F
推
08/03 06:49,
6年前
, 6F
08/03 06:49, 6F
推
08/03 23:04,
6年前
, 7F
08/03 23:04, 7F
→
08/04 02:28,
6年前
, 8F
08/04 02:28, 8F
→
08/04 02:29,
6年前
, 9F
08/04 02:29, 9F
推
08/04 05:26,
6年前
, 10F
08/04 05:26, 10F
→
08/04 05:26,
6年前
, 11F
08/04 05:26, 11F
→
08/04 05:27,
6年前
, 12F
08/04 05:27, 12F
推
08/04 05:31,
6年前
, 13F
08/04 05:31, 13F
→
08/04 05:31,
6年前
, 14F
08/04 05:31, 14F
推
08/04 05:35,
6年前
, 15F
08/04 05:35, 15F
→
08/04 05:35,
6年前
, 16F
08/04 05:35, 16F
→
08/04 05:36,
6年前
, 17F
08/04 05:36, 17F
討論串 (同標題文章)
Python 近期熱門文章
PTT數位生活區 即時熱門文章