Home  >  Article  >  Backend Development  >  python 获取文件列表(或是目录例表)

python 获取文件列表(或是目录例表)

WBOY
WBOYOriginal
2016-06-06 11:26:581780browse

import os
os.os.listdir(path)
然后再一个一个的分析文件和目录
通过和dos命令dir的巧妙结合,可以很轻松的做到这点,看示例
获取目录下所有文件方法
cmd = "dir /A-D /B"
list_file = os.popen(cmd).readlines()
cmd命令解释为 /A-D 选取所有的非目录文件列表 /B 只使用空格分隔开所有的文件名
在通过readlines方法,获取到包含所有文件的list,这样就得到指定目录下的所有文件了
同时附上获取所有目录的方法:
cmd = "dir /AD /B"
list_file = os.popen(cmd).readlines()

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn