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

看板Python作者 (艾爾文)時間6年前 (2019/08/02 22:52), 編輯推噓6(6011)
留言17則, 3人參與, 6年前最新討論串2/4 (看更多)
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
Python 很重視縮排的。所有的縮排要正確
08/03 02:02, 5F

08/03 06:49, 6年前 , 6F
呃...原本的代碼就已經判斷不了圖片是否完整了...
08/03 06:49, 6F

08/03 23:04, 6年前 , 7F
可能複製bbs的程式碼直接貼上去? 這樣很容易縮排錯誤
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
現在頭痛的是當os.walk拿來當str的時候 只能讀取最後一
08/04 05:35, 15F

08/04 05:35, 6年前 , 16F
行 網路上沒有找到相似的東西能讓我改...
08/04 05:35, 16F

08/04 05:36, 6年前 , 17F
呃...我應該沒搞錯str的意思吧...
08/04 05:36, 17F
文章代碼(AID): #1TH4v3Oq (Python)
文章代碼(AID): #1TH4v3Oq (Python)