• 遍历系统中某一目录下的所有文件名
#! /usr/bin/python
# coding:utf-8 import os def dirList(path):
filelist = os.listdir(path)
print filelist dirList('/Users/spdbma/python') 结果: spdbmadeMacBook-Pro:python spdbma$ python dirlist.py
['.DS_Store', 'CharPainting', 'crawler', 'dirlist.py', 'picknames', 'test.py']
  • 遍历系统中某一绝对路径下的所有文件名(包含路径显示)
#! /usr/bin/python
# coding:utf-8 import os def dirList(path):
filelist = os.listdir(path)
allfile = []
for filename in filelist:
filepath = os.path.join(path,filename)
if os.path.isdir(filepath):
dirList(filepath)
allfile.append(filepath)
return allfile allfile = dirList('/Users/spdbma/python')
print all file 结果: spdbmadeMacBook-Pro:python spdbma$ python dirlist.py
['/Users/spdbma/python/.DS_Store', '/Users/spdbma/python/CharPainting', '/Users/spdbma/python/crawler', '/Users/spdbma/python/dirlist.py', '/Users/spdbma/python/picknames', '/Users/spdbma/python/test.py']
05-01 02:08