Python小点dian儿: 读取一个目录下目录和文件

本文介绍了如何使用Python的os模块遍历目录,包括输出文件目录、获取单层文件名、获取所有层文件路径以及按指定后缀筛选文件。提供了五个代码示例,详细解释了每段代码的功能和用途。
摘要由CSDN通过智能技术生成

目录

 

代码一: 输出文件目录相关内容

代码二:输出一个目录下所有文件名字(第一层)

代码三:返回当前路径下所有层的文件路径列表

代码四:依次从内层到外层获取当前路径下文件(改造自代码三),也可直接代码一获取

代码五:获取当前路径下一层指定后缀文件列表(多层可改造代码三)


代码一: 输出文件目录相关内容

 os.walk(file_dir)从外到内获取多层

# -*- coding: utf-8 -*-   
      
    import os  
      
    def file_name(file_dir):   
        for root, dirs, files in os.walk(file_dir):  
            print(root) #当前目录路径  
            print(dirs) #当前路径下所有子目录  
            print(files) #当前路径下所有非目录子文件

代码一会从外到内,将文件夹三个内容一层一层的输出。

代码二:输出一个目录下所有文件名字(第一层)

os.listdir(rootdir) 当前层

 os.path.isfile(list[i]):#判断是否是文件

#获取一个路径下第一层,所有文件名列表
def get_filenames(file_dir):
    L=[]
    list = os.listdir(rootdir) #列出文件夹下所有的目录与文件
    for i in range(0,len(list)):
        if os.path.isfile(list[i]):#判断是否是文件
            L.append(os.path.join(file_dir,list[i]))

代码三:返回当前路径下所有层的文件路径列表

os.path.isdir(file_path)判断是否是目录(文件夹)

# -*- coding: utf-8 -*-
import os

def listdir(path, list_name):  #传入存储的list
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        if os.path.isdir(file_path):
            listdir(file_path, list_name)
        else:
            list_name.append(file_path)
    print(list_name)#虽然打印出来,但是最后的打印才是return的最后结果
    return list_name

list_name=[] #需要在外层定义,才能获取当前路径所有文件名,试想在内层定义会如何
list_name=listdir(rootdir, list_name)#返回当前路径下所有文件路径列表

#['E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\10_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\11_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\12_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\13_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\14_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\15_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\16_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\17_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\18_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\19_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\1_24\\10_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\1_24\\11_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\1_24\\12_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\1_24\\13_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\1_24\\14_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\1_24\\15_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\1_24\\16_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\1_24\\17_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\1_24\\18_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\1_24\\19_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\1_24\\1_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\1_24\\20_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\1_24\\21_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\1_24\\22_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\1_24\\24_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\1_24\\2_fact.txt', 'E:\\Mypython3\\wenshu_my\\data_my\\fen_200\\1_24\\3_fact.txt', 'E:\\Mypython3\\
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值