試著把內容印出來就會找到解決方案。
import os
import imghdr
path = 'Z://oswalk'
for root, dirs, files in os.walk(path):
for f in files:
print(os.path.join(root, f), imghdr.what(os.path.join(root, f)))
https://i.imgur.com/uUssjxt.jpg
※ 引述《s4028600 (佑)》之銘言:
: import os
: path = 'D:\long\Desktop\image\\'
: def is_valid_jpg(jpg_file):
: with open(jpg_file, 'rb') as f:
: f.seek(-2, 2)
: buf = f.read()
: return buf == b'\xff\xd9'
: def is_valid_png(png_file):
: with open(png_file, 'rb') as f:
: f.seek(-2, 2)
: buf = f.read()
: return buf == b'\x60\x82'
: for file in os.listdir(path):
: pic_file = path + file
: isJpg = is_valid_jpg(pic_file)
: isPng = is_valid_png(pic_file)
: print("jpeg : %s, png %s, file %s " % (isJpg, isPng, file))
: 之前找到的就算能讀取
: 也無法正確判斷
: 這是我重新找到的
: 稍微修改成一個資料夾下的圖片
: 問題來了
: 我如果要讀取一個資料夾下的所有子資料夾下的圖片
: 要怎麼改
: 原本是打算用glob.glob結果失敗...