python遍历一个目录,输出所有的文件名

来源:互联网 发布:西安网络电视在线直播 编辑:程序博客网 时间:2024/05/21 15:03

python 获取一个文件夹内(包括子文件夹)所有文件的名字和路径

import osdir = "e:\\"for root, dirs, files in os.walk(dir):    for file in files:        print os.path.join(root,file)

或:

import ospath = r'e:\case'fns = [os.path.join(root,fn) for root, dirs, files in os.walk(path) for fn in files]for f in fns:    print(f)print(len(fns))
#coding=utf-8import osdef GetFileList(dir, fileList):    newDir = dir    if os.path.isfile(dir):        fileList.append(dir.decode('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 fileListlist = GetFileList('D:\\workspace\\PyDemo\\fas', [])for e in list:    print e
1 0
原创粉丝点击