zoukankan      html  css  js  c++  java
  • 遍历某个文件夹下所有的文件

    通过递归遍历所有的目录+子目录(这个问题我经常会搞不明白,希望大家能用的清楚)

    1、直接显示所有文件

    def showAllFiles(path):
    
        list_all =os.listdir(path)
    
        for one in list_all:
    
            one_path =os.path.join(path,one)
    
            if os.path.isfile(one_path):
                print(one)          #只显示文件名称
                # print(one_path)   #如果需要连带路径一起显示
            else:
                showAllFiles(one_path)  #如果当前子文件是目录,将目录重新进行判断

    2、如果需要返回一个list

    对上面的方法进行改造,但是有个注意的点,list_file=[]这个空的list_file必须在方法的外部,

    list_file =[]
    
    def showAllFiles1(path,list_file):
      
       #list_file =[]  #不能放在内部,否则如果子文件是目录,那么重新调用showAllFile1()时又会初始化为空,前面的数据就没有了
        list_all =os.listdir(path)
    
        for one in list_all:
    
            one_path =os.path.join(path,one)
    
            if os.path.isfile(one_path):
                list_file.append(one)
            else:
                showAllFiles1(one_path)  #如果当前子文件是目录,将目录重新进行判断
    
        return list_file
    
    

    方法2: os.path.walk()






  • 相关阅读:
    Bluedroid介绍
    Android蓝牙介绍
    Android Bluetooth抓包
    Bluetooth LMP介绍
    Bluetooth Baseband介绍
    Bluetooth SDP介绍
    Bluetooth HFP介绍
    Bluetooth RFCOMM介绍
    Bluetooth L2CAP介绍
    Windows开发
  • 原文地址:https://www.cnblogs.com/jesse-zhao/p/14492459.html
Copyright © 2011-2022 走看看