Gal*_*nap 7 python operating-system
是否可以使用os.listdir命令查看具有特定扩展名的文件?我希望它能够工作,因此最终可能只显示带有.f的文件或文件夹.我检查了文档,发现什么都没有,所以不要问.
Ned*_*der 21
glob 擅长这个:
import glob
for f in glob.glob("*.f"):
print f
Run Code Online (Sandbox Code Playgroud)
Dav*_*d Z 11
不要问什么?
[s for s in os.listdir() if s.endswith('.f')]
Run Code Online (Sandbox Code Playgroud)
如果要查看扩展列表,可以进行明显的概括,
[s for s in os.listdir() if s.endswith('.f') or s.endswith('.c') or s.endswith('.z')]
Run Code Online (Sandbox Code Playgroud)
或者另一种方式是写一点:
[s for s in os.listdir() if s.rpartition('.')[2] in ('f','c','z')]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13982 次 |
| 最近记录: |