首页 > 编程语言 >python 文件夹遍历三种方法

python 文件夹遍历三种方法

时间:2023-08-09 13:44:40浏览次数:50  
标签:files 遍历 python lst 文件夹 file print path os

os.listdir(path),返回path目录下的文件夹和文件,但不包含子文件夹里的文件夹和文件

递归遍历所有文件

import os

def recursive_listdir(path):

    files = os.listdir(path)
    for file in files:
        file_path = os.path.join(path, file)

        if os.path.isfile(file_path):
            print(file)

        elif os.path.isdir(file_path):
          recursive_listdir(file_path)

recursive_listdir(r'./test')

os.walk(top, topdown=True, one rror=None, followlinks=False),生成目录树中的文件夹名和文件名。

显示所有子目录

import os

paths = os.walk(r'./test')

for path, dir_lst, file_lst in paths:
    for dir_name in dir_lst:
        print(os.path.join(path, dir_name))

显示目录下所有文件

import os

paths = os.walk(r'./test')

for path, dir_lst, file_lst in paths:
    for file_name in file_lst:
        print(os.path.join(path, file_name))

os.scandir 高效

遍历目录树

import os

def traversal_files(path):
    for item in os.scandir(path):
        if item.is_dir():
          dirs.append(item.path)

        elif item.is_file():
          files.append(item.path)

    print('dirs:')
    print('\n'.join(dirs))

    print()

    print('files:')
    print('\n'.join(files))

traversal_files(r'./test')

 

标签:files,遍历,python,lst,文件夹,file,print,path,os
From: https://www.cnblogs.com/duoruaimi4/p/17616653.html

相关文章

  • Mac输入 python 打开 python3
    往.bash_profile加入此配置echo'aliaspython="python3"'>>.bash_profile调用source~/.bash_profile生效配置打开open.zshrc添加source~/.bash_profile调用source~/.bash_profile生效配置......
  • python多线程学习记录
    Python多线程参考文章:python多线程详解(超详细)、Python线程池(threadpool)创建及使用+实例代码、第二十章多线程1、多线程的概念2、python多线程的基本使用方法3、多线程的优点及与多进程的关系1、多线程的概念线程也叫轻量级进程,是操作系统能够进行运算调度......
  • python简述十大排序
    小阿杰已经摆烂了很多天了QAQ,今天决定氵一篇新博客(......
  • python贪吃蛇
    importsysimportmsvcrtimportthreadingimporttimeimportmapimportosimportplayerglobalgo_to,fresh_timego_to="s"fresh_time=0.1defstop(x,y):temp=map.map()iftemp.good_game(x,y):sys.exit(0)defAuto():global......
  • 【Python】日期格式转换 <字符串、datetime、date>
    #coding:utf-8importdatetimefromdateutil.parserimportparsefromdateutil.relativedeltaimportrelativedeltafromloguruimportloggeraslogsclassdate_transform:"""日期格式转换"""defauto(self,d):"......
  • 基于Python的网上图书商城
    电子商城作为一个竞争激烈的市场销售方式,大多数电子商城的管理者都主要考虑降低成本,提升商城服务满意度。一年一度的双十一、双十二,给众多的消费者带来了购物盛宴,也给各种商城提供了巨大的销售额。本文通过调研网上图书商城的业务流程,审慎分析搭建一个基于PythonDjango的网上图书商......
  • 基于Python的京东商品信息分析
    系统设计思想京东网站是通过服务端进行数据动态展示的,这样就可以通过网页上的源代码分析网页界面上看到的数据信息,不仅如此,还可以通过在采集京东网页数据的时候,动态读取URL地址。京东页面数据更新的时候,或者读取下一页面的时候,数据显示是异步加载的。这种加载数据的方法能够实时的......
  • python语言学习-------------------xlwt模块
    xlwt模块是Python中一个用于管理Excel文件的模块,用以将数据以Excel表格的形式写入到Excel文件中。它具有操作方便、支持多种Excel文件格式等优点,因此,被广泛应用于数据处理、表格导出等方面。1.创建Excel文件使用xlwt模块创建并打开Excel文件:importxlwt#创建workbook对象w......
  • python实现创建一个银行类,这个类实现了两个方法,第一个方法可以将用户信息写入到文件中
    classbank:defuser_info(self):a=input('请输入用户信息:')#不写encoding='utf-8'中文会乱码withopen('info.txt','w',encoding='utf-8')asf:f.write(a)defget_info(se......
  • python:安装backgroundremover 0.2.4(Python 3.9.16)
    一,官方代码及文档:https://github.com/nadermx/backgroundremover二,安装backgroundremover的准备工作1,安装pytorch:https://blog.imgtouch.com/index.php/2023/08/03/rocky-linux-an-zhuang-pytorch-pytorch-2-1-python-3-9-16/2, 安装ffmpeg:https://blog.imgtouch.......