python遍历一个目录,输出所有文件名
python
os模块
os
- import os
- def GetFileList(dir, fileList):
- newDir = dir
- if os.path.isfile(dir):
- fileList.append(dir.encode('gbk'))
- elif os.path.isdir(dir):
- for s in os.listdir(dir):
- #如果需要忽略某些文件夹,使用以下代码
- #if s == "xxx":
- #continue
- newDir=os.path.join(dir,s)
- GetFileList(newDir, fileList)
- return fileList
- list = GetFileList('C:pyqt4/', [])
- for e in list:
- print (e)
- 代码取自百度知道:http://zhidao.baidu.com/link?url=xbhSxfsy_23SzDkGHs-9Gazw5R_SyVFpQvaKvIyZAf8IBrF12wKzsZicx4TDLyPAAzWJpscFndpLse61LKgr9EH0ESxnh6HWUri5101u1FG