Discuss / Python / 求助

求助

Topic source

开心Ikos

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

import os lj=[x for x in os.listdir('.') if os.path.isdir(x)] lj1=[x for x in os.listdir('d:\python') if os.path.isdir(x)] lj2=[x for x in os.listdir('d:\python')] print(lj) print(lj1) print(lj2)

为什么lj可以正常过滤,只取目录文件,lj1 结果是空[],lj2可以正常返回目录下所有文件和目录

-SuperDon-

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

次级目录下首先要 join()一下才能识别:

allFiles = [os.path.join(d,f) for f in os.listdir(d)]

  • 1

Reply