import imghdr
import os
root=["D:\long\Desktop"]
for path in root:
for dirPath, dirNames, fileNames in os.walk(path):
for file in fileNames:
pic_file=os.path.join(dirPath,file)
img = imghdr.what(pic_file)
#print(os.path.splitext(pic_file)[1])
if os.path.splitext(pic_file)[1].lstrip('.') != img:
#print(img,pic_file)
#print(os.path.splitext(pic_file)[0])
name=os.path.splitext(pic_file)[0]+'.'+img
print(name)
os.rename(pic_file,name)
有几个问题
1.我所用的imghdr似乎只能辨认图片的副档名
它把我的txt当错误的档案
有能辨识其他副档名是否正确的包吗
2.我用其他程式辨识的是jpg但是imghdr的辨识却是jpeg
3.遇到非图片的会错误比如txt
不过有打算用try所以算是小问题