zoukankan      html  css  js  c++  java
  • Python 遍历一个目录,输出所有的文件名

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

    import os
    dir = "e:\"
    for root, dirs, files in os.walk(dir):
        for file in files:
            print os.path.join(root,file)
    • 1
    • 2
    • 3
    • 4
    • 5

    或:

    import os
    path = 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))
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    #coding=utf-8
    import os
    
    def 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 fileList
    
    list = GetFileList('D:\workspace\PyDemo\fas', [])
    for e in list:
        print e
  • 相关阅读:
    Libvirt错误总结
    linux学习
    HMC 命令行登陆设置
    AIX扩VG和扩文件系统
    Bootstrap CSS2
    Bootstrap CSS
    JQuery的实例集合
    JQuery的noConflict()方法
    android的intent实现页面的跳转
    android的activity
  • 原文地址:https://www.cnblogs.com/gina11/p/14098420.html
Copyright © 2011-2022 走看看