Re: [問題] 讀取下一個資料夾路徑

看板Python作者 (艾爾文)時間6年前 (2019/08/04 16:32), 6年前編輯推噓13(13016)
留言29則, 4人參與, 6年前最新討論串3/4 (看更多)
其實幫人實做不是一個好做法,除非對方己經束手無策。不過我感覺你很不用心,同樣的問題回了又問,還說別人的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

08/04 16:51, 6年前 , 1F
推 好人難當
08/04 16:51, 1F

08/04 20:04, 6年前 , 2F
抱歉因為我的理解力確實很差 不過也的確學了一些東西
08/04 20:04, 2F

08/04 20:04, 6年前 , 3F
至少終於搞懂os.walk的一些用法 雖然還是很多概念完全
08/04 20:04, 3F

08/04 20:04, 6年前 , 4F
搞不懂...
08/04 20:04, 4F

08/04 20:13, 6年前 , 5F
現在沒電腦晚點在試
08/04 20:13, 5F

08/04 20:18, 6年前 , 6F
很多沒看過的代碼...不知道有沒有那種看程式跑的過程來
08/04 20:18, 6F

08/04 20:18, 6年前 , 7F
學習...像這個不知道是怎麼圖像化的...
08/04 20:18, 7F

08/04 20:20, 6年前 , 8F

08/04 20:20, 6年前 , 9F
6faQNTgw1f5wnm06h3ug30ci08cake.gif
08/04 20:20, 9F

08/04 20:27, 6年前 , 10F
rootDirLists是多個資料夾?如果是同目錄下的多個子目
08/04 20:27, 10F

08/04 20:27, 6年前 , 11F
錄我想我終於找到方法了...
08/04 20:27, 11F

08/04 21:38, 6年前 , 12F
經過測試後有幾個問題
08/04 21:38, 12F

08/04 21:40, 6年前 , 13F
你這個搜尋檔案的能力比我用walk轉到txt再讀取使用還好
08/04 21:40, 13F

08/04 21:40, 6年前 , 14F
我整個白寫了...
08/04 21:40, 14F

08/04 21:41, 6年前 , 15F
count...沒人建議我用這個 第一次看到 不知道怎麼問...
08/04 21:41, 15F

08/04 21:44, 6年前 , 16F
對了 你的圖片沒問題 但是你的代碼有幾行被切斷了 可能
08/04 21:44, 16F

08/04 21:44, 6年前 , 17F
是排版失誤吧
08/04 21:44, 17F

08/04 21:56, 6年前 , 18F
有些檔案被誤判為錯誤或沒找到...
08/04 21:56, 18F

08/04 22:01, 6年前 , 19F
關於那個for in ...好吧目前看不懂 不過有個參考可以分
08/04 22:01, 19F

08/04 22:01, 6年前 , 20F
析了
08/04 22:01, 20F

08/04 22:24, 6年前 , 21F
lower是大寫轉小寫 這個是用在什麼情況? 刪掉好像不會
08/04 22:24, 21F

08/04 22:24, 6年前 , 22F
影響程式?
08/04 22:24, 22F

08/05 01:45, 6年前 , 23F
檔案名稱有可能是大寫。全部先改成小寫
08/05 01:45, 23F

08/05 01:46, 6年前 , 24F
我建議你先學習怎麼分析你的問題然後做出流程圖
08/05 01:46, 24F

08/05 01:49, 6年前 , 25F
以圖為準吧。文字只是幫你少打些字。
08/05 01:49, 25F

08/05 01:51, 6年前 , 26F
程式沒有很完整的測試。只是分享供你參考。我現在看到就
08/05 01:51, 26F

08/05 01:51, 6年前 , 27F
有一、兩個缺點,留給你想想
08/05 01:51, 27F

08/05 01:55, 6年前 , 28F
寫程式過程比結果重要
08/05 01:55, 28F

08/05 13:15, 6年前 , 29F
推好人
08/05 13:15, 29F
文章代碼(AID): #1THfWAHE (Python)
文章代碼(AID): #1THfWAHE (Python)