作者alvinlin (艾爾文)
看板Python
標題Re: [問題] 讀取下一個資料夾路徑
時間Sun Aug 4 16:32:08 2019
其實幫人實做不是一個好做法,除非對方己經束手無策。不過我感覺你很不用心,同樣的問題回了又問,還說別人的code沒解決你的問題。這是當然的啊。每個人需求不同,至少他解決了他的問題還分享給你。
我也是初學者,只是剛開始這個問題我是用HEX Editor 去研究了檔案的 binary code,對照檔案本身的解碼表一個個去解碼。透過這個過程也學到了很多不同的方法和函式。如果對我有幫助的,我就當練習自己實做了一次。
以下綜整這些日子以來所有的技巧。除了jpg和png外的圖檔不檢查視為正常。格式與副檔名不符的直接修改。提供有類似問題的朋友參考。
import os
import shutil
from PIL import Image
import imghdr
def is_img_valid(imgFile):
isValid = True
try:
Image.open(imgFile).verify()
except:
isValid = False
return isValid
def is_valid_jpg_or_png(imgFile):
if currentFile.endswith('jpeg') or currentFile.endswith('jpg'):
try:
with open(imgFile, 'rb') as f:
f.seek(-2, 2)
return f.read() == b'\xff\xd9'
except OSError:
return False
elif currentFile.endswith('png'):
try:
with open(imgFile, 'rb') as f:
f.seek(-3, 2)
return f.read() == b'\x60\x82\x00' or f.read() [1:] == b'\x60\x82'
except OSError:
return False
else:
return True
rootDirLists = ['Z://folder1', 'C://folder2', 'D://folder3']
count=0
corruptList =[]
formatIncorrect = []
for rootDir in rootDirLists:
print('============尋找目錄 {} 中的圖檔 ============: '.format(rootDir))
for root, dirs, files in os.walk(rootDir):
for file in files:
count += 1
currentFile = os.path.join(root, file).lower()
imgFormat = imghdr.what(currentFile)
if imgFormat != None:
print("檢查第{}個檔案中...檔名為:[{}], 檔頭格式為 [{}], ".format(count, currentFile, imgFormat), end="")
if not is_img_valid(currentFile):
corruptList.append(currentFile)
result = "看來是壞了"
else:
fileExt = currentFile.split('.')[-1]
if fileExt[0:2] not in imgFormat:
formatIncorrect.append(currentFile)
correctedFile = currentFile.replace(currentFile.split('.
print("副檔名與檔頭格式不符, 直接更改檔案名稱為: [{}], "
os.rename(currentFile, correctedFile)
currentFile = correctedFile
if is_valid_jpg_or_png(currentFile):
result = "看起來沒問題"
else:
corruptList.append(currentFile)
result = "看來是壞了"
print(" 檢查結果 [{}]".format(result))
print("============結果 [目錄={}] == [檢查檔案數={}] ============ ".format(rootD
if corruptList:
print('列出壞掉的檔案:')
for i in range(0, len(corruptList)):
print(corruptList[i])
else:
print('沒有壞掉的檔案.')
——-
請看圖以免格式跑掉了
https://i.imgur.com/dmPruPn.jpg
https://i.imgur.com/Ri7f793.jpg
※ 引述《alvinlin (艾爾文)》之銘言:
: hi, 我想說應該不會再有問題了.
: 不知你有試了我寄給你的那個連結的範例了嗎? [後刪]
: ※ 引述《s4028600 (佑)》之銘言:
: : 簡單來說
: : 我已經用os.walk列出所有子資料夾的路徑
: : 然後當作參數進行讀取
: : 但是讀取卻只會讀取列出來的最後一條
: : 我要如何讓他讀取下一個路徑
: : 有相關的教學嗎
: : 網路上只有找到列出路徑
: : 找不到將這些列出的路徑拿來用的方法
: : 求詳細...
-----
Sent from JPTT on my iPhone
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.192.186.172 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1564907530.A.44E.html
※ 編輯: alvinlin (123.192.186.172 臺灣), 08/04/2019 16:40:26
※ 編輯: alvinlin (123.192.186.172 臺灣), 08/04/2019 16:44:45
推 HenryLiKing: 推 好人難當 08/04 16:51
推 s4028600: 抱歉因為我的理解力確實很差 不過也的確學了一些東西 08/04 20:04
→ s4028600: 至少終於搞懂os.walk的一些用法 雖然還是很多概念完全 08/04 20:04
→ s4028600: 搞不懂... 08/04 20:04
推 s4028600: 現在沒電腦晚點在試 08/04 20:13
推 s4028600: 很多沒看過的代碼...不知道有沒有那種看程式跑的過程來 08/04 20:18
→ s4028600: 學習...像這個不知道是怎麼圖像化的... 08/04 20:18
→ s4028600: 6faQNTgw1f5wnm06h3ug30ci08cake.gif 08/04 20:20
推 s4028600: rootDirLists是多個資料夾?如果是同目錄下的多個子目 08/04 20:27
→ s4028600: 錄我想我終於找到方法了... 08/04 20:27
推 s4028600: 經過測試後有幾個問題 08/04 21:38
推 s4028600: 你這個搜尋檔案的能力比我用walk轉到txt再讀取使用還好 08/04 21:40
→ s4028600: 我整個白寫了... 08/04 21:40
→ s4028600: count...沒人建議我用這個 第一次看到 不知道怎麼問... 08/04 21:41
推 s4028600: 對了 你的圖片沒問題 但是你的代碼有幾行被切斷了 可能 08/04 21:44
→ s4028600: 是排版失誤吧 08/04 21:44
推 s4028600: 有些檔案被誤判為錯誤或沒找到... 08/04 21:56
推 s4028600: 關於那個for in ...好吧目前看不懂 不過有個參考可以分 08/04 22:01
→ s4028600: 析了 08/04 22:01
推 s4028600: lower是大寫轉小寫 這個是用在什麼情況? 刪掉好像不會 08/04 22:24
→ s4028600: 影響程式? 08/04 22:24
→ alvinlin: 檔案名稱有可能是大寫。全部先改成小寫 08/05 01:45
→ alvinlin: 我建議你先學習怎麼分析你的問題然後做出流程圖 08/05 01:46
→ alvinlin: 以圖為準吧。文字只是幫你少打些字。 08/05 01:49
→ alvinlin: 程式沒有很完整的測試。只是分享供你參考。我現在看到就 08/05 01:51
→ alvinlin: 有一、兩個缺點,留給你想想 08/05 01:51
→ alvinlin: 寫程式過程比結果重要 08/05 01:55
推 ouryouth: 推好人 08/05 13:15