下面两段代码为什么结果不一样呢?
E盘下的两个文件夹和两个文件,为什么第一段运行完,结果不正确呢?#第一段
import os
files = os.listdir('e:')
for eachfile in files:
if os.path.isdir(eachfile):
print(eachfile, 'is folder')
else:
print(eachfile, 'is file')
#第二段
import os
os.chdir('e:')
files = os.listdir()
for eachfile in files:
if os.path.isdir(eachfile):
print(eachfile, 'is folder')
else:
print(eachfile, 'is file') 哪位能帮忙解答一下? os.chdir('e:')我正好在学,不切换目录,程序一定出来的是错的结果。
os.path.isdir(eachfile) 这个判断只针对当前目录,不是当前目录,你list出来的东东,结果一定是F,所以会全部打成 is a file
页:
[1]