zoukankan      html  css  js  c++  java
  • python 读取一个目录下的所有目录和文件

    #!/usr/bin/python  
    # -*- coding:utf8 -*-  
      
    import os  
    allFileNum = 0  
    def printPath(level, path):  
        global allFileNum  
        ''''' 
        打印一个目录下的所有文件夹和文件 
        '''  
        # 所有文件夹,第一个字段是次目录的级别  
        dirList = []  
        # 所有文件  
        fileList = []  
        # 返回一个列表,其中包含在目录条目的名称(google翻译)  
        files = os.listdir(path)  
        # 先添加目录级别  
        dirList.append(str(level))  
        for f in files:  
            if(os.path.isdir(path + '/' + f)):  
                # 排除隐藏文件夹。因为隐藏文件夹过多  
                if(f[0] == '.'):  
                    pass  
                else:  
                    # 添加非隐藏文件夹  
                    dirList.append(f)  
            if(os.path.isfile(path + '/' + f)):  
                # 添加文件  
                fileList.append(f)  
        # 当一个标志使用,文件夹列表第一个级别不打印  
        i_dl = 0  
        for dl in dirList:  
            if(i_dl == 0):  
                i_dl = i_dl + 1  
            else:  
                # 打印至控制台,不是第一个的目录  
                print '-' * (int(dirList[0])), dl  
                # 打印目录下的所有文件夹和文件,目录级别+1  
                printPath((int(dirList[0]) + 1), path + '/' + dl)  
        for fl in fileList:  
            # 打印文件  
            print '-' * (int(dirList[0])), fl  
            # 随便计算一下有多少个文件  
            allFileNum = allFileNum + 1  
      
    if __name__ == '__main__':  
        printPath(1, '/home/lizheng')  
        print '总文件数 =', allFileNum

    转载至https://www.cnblogs.com/flyhigh1860/p/3896111.html

  • 相关阅读:
    python编码基础知识
    python编码问题
    python中文乱码
    mysql sql灵活运用
    MySQL函数讲解(MySQL函数大全)
    python:UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xef in position xxx: ordinal not in range(128)
    sql replace into 与 insert into
    微信布局
    盘点六大在中国复制失败的O2O案例
    字符串问题(一)
  • 原文地址:https://www.cnblogs.com/aaronthon/p/9368511.html
Copyright © 2011-2022 走看看