Discuss / Python / .jpg文件在os.path.isfile()函数中判定该为false?

.jpg文件在os.path.isfile()函数中判定该为false?

Topic source

H_ymin

#1 Created at ... [Delete] [Delete and Lock User]
#该路径包含一些jpg文件
s1 = os.listdir(r'C:\Users\hymn\Desktop\Output')
print (os.path.isfile(s1[0]))

为什么打印出来是False?导致

print ([x for x in os.listdir(r'C:\Users\hymn\Desktop\Output') if os.path.isfile(x) and os.path.splitext(x)[1] == '.jpg']

也无法使用(os.pat.isfile(x)判定为false,打印出来一个空list) 奇怪的是换成本地目录

print ([x for x in os.listdir('.') if os.path.isfile(x) and os.path.splitext(x)[1] == '.jpg'])

就可以了

求解!

廖雪峰

#2 Created at ... [Delete] [Delete and Lock User]

listdir()只返回文件名,不包含完整路径,你在其他目录下测试isfile('a.jpg')当然是False了

用os.path.isfile(os.path.join(currentpath, filename))


  • 1

Reply