请问isdir()为什么不能判断出是否是文件夹?
F盘这些全部为文件夹,为啥在for循环中用isdir()判断无法打印?import os
all_files = os.listdir('F:\\')
print(all_files)#只输出了这个
for each_file in all_files:
if os.path.isdir(each_file):
print(each_file)
输出:
['$RECYCLE.BIN', '2017 Travel in Europe', '2018 EAST COAST LVY SUMMER PROGRAM', 'CET-6', 'Clinical Lessons', 'CloudMusic', 'Innovation', 'kinggsoft', 'Korean', 'Mandarin', 'NCRE', 'OneDrive', 'OneDriveTemp', 'Others', 'Photograph', 'Python', 'qqpcmgr_docpro', 'School Lessons', 'Summer Camp', 'System Volume Information', 'TOEFL', '实习', '科研', '综测', '考研'] if os.path.isdir(r'F:\' + each_file): suchocolate 发表于 2020-4-21 17:00
if os.path.isdir(r'F:\' + each_file):
为什么要这样改呢 suchocolate 发表于 2020-4-21 17:00
if os.path.isdir(r'F:\' + each_file):
而且
all_files = os.listdir('F:\\python')
这个也不行 python这个文件夹下也是有文件夹的
但是
all_files = os.listdir('F:\\python\\jupyterproject')
这样就可以打印了
这是为什么呢 这几个有什么区别吗 本帖最后由 suchocolate 于 2020-4-21 17:07 编辑
yangxuebabe 发表于 2020-4-21 17:02
为什么要这样改呢
py脚本默认工作路径是当前py所在路径,而上面listdir时应该不是脚本当前的路径。
所以for循环判断时,因为当前路径没有这些文件夹,所以都是false,就都不会print。
另外的办法就是在第二行加1句:
os.chdir(r'F:\') suchocolate 发表于 2020-4-21 17:05
py脚本默认工作路径是当前py所在路径,而上面listdir时应该不是脚本当前的路径。
所以for循环判断时, ...
啊!懂了!感谢 yangxuebabe 发表于 2020-4-21 17:05
而且
all_files = os.listdir('F:\\python')
这个也不行 python这个文件夹下也是有文件夹的
我漏了一个斜杠,两个斜杠
if os.path.isdir(r'F:\\' + each_file):
页:
[1]