侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Python列出一个文件夹及其子目录的所有文件

Python  /  管理员 发布于 7年前   194

python简介

Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。

Python由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。

像Perl语言一样, Python 源代码同样遵循 GPL(GNU General Public License)协议。

>>> import os>>> for i in os.walk("."):... print i[0],"\n##",i[1],"\n##",i[2]... . #当前目录## ['fa', 'out'] #当前目录中的子目录 ## ['meta_rna.sh', 'nohup.out', 'log.cpu', 'blast_seq.py']./fa # 第一个子目录## [] # 第一个子目录中的目录## ['assemblyar_new_2.faa']./out # 第二个子目录## [] # 第二个子目录中的目录## ['assemblyar_new_2.faa.coord', 'assemblyar_new_2.faa.mask', 'assemblyar_new_2.faa.seq', 'result_1.xm', 'result.xml', 'blast_seq.py']

也可以用 os.path.walk, 先定义一个访问文件夹的函数, VisitDir

>>> def VisitDir(arg, dirname, names):... for filespath in names:... print os.path.join(dirname, filespath)... >>> path=".">>> os.path.walk(path, VisitDir, ())./meta_rna.sh./fa./out./nohup.out./log.cpu./blast_seq.py./fa/assemblyar_new_2.faa./out/assemblyar_new_2.faa.coord./out/assemblyar_new_2.faa.mask./out/assemblyar_new_2.faa.seq./out/result_1.xm./out/result.xml./out/blast_seq.py>>> os.getcwd()'/home/served_pro/Find_nick'>>> abs_path= os.getcwd()>>> os.path.walk(abs_path, VisitDir, ())/home/served_pro/Find_nick/meta_rna.sh/home/served_pro/Find_nick/fa/home/served_pro/Find_nick/out/home/served_pro/Find_nick/nohup.out/home/served_pro/Find_nick/log.cpu/home/served_pro/Find_nick/blast_seq.py/home/served_pro/Find_nick/fa/assemblyar_new_2.faa/home/served_pro/Find_nick/out/assemblyar_new_2.faa.coord/home/served_pro/Find_nick/out/assemblyar_new_2.faa.mask/home/served_pro/Find_nick/out/assemblyar_new_2.faa.seq/home/served_pro/Find_nick/out/result_1.xm/home/served_pro/Find_nick/out/result.xml/home/served_pro/Find_nick/out/blast_seq.py

下面给大家介绍python列出文件夹下的所有文件

#方法1:使用os.listdirimport osfor filename in os.listdir(r'c:\\windows'):print filename#方法2:使用glob模块,可以设置文件过滤import globfor filename in glob.glob(r'c:\\windows\\*.exe'):print filename#方法3:通过os.path.walk递归遍历,可以访问子文件夹import os.pathdef processDirectory ( args, dirname, filenames ):print 'Directory',dirnamefor filename in filenames:print ' File',filenameos.path.walk(r'c:\\windows', processDirectory, None )#方法4:非递归import osfor dirpath, dirnames, filenames in os.walk('c:\\\\winnt'):print 'Directory', dirpathfor filename in filenames:print ' File', filename

另外,判断文件与目录是否存在:

import osos.path.isfile('test.txt') #如果不存在就返回Falseos.path.exists(directory) #如果目录不存在就返回False

以上所述是小编给大家介绍的Python列出一个文件夹及其子目录的所有文件,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!

您可能感兴趣的文章:

  • python获得文件创建时间和修改时间的方法
  • 用Python删除本地目录下某一时间点之前创建的所有文件的实例
  • python实现删除文件与目录的方法
  • Python实现读取目录所有文件的文件名并保存到txt文件代码
  • Python中的文件和目录操作实现代码
  • python获取指定目录下所有文件名列表的方法
  • python获取文件后缀名及批量更新目录下文件后缀名的方法
  • Python读取一个目录下所有目录和文件的方法
  • Python获取文件所在目录和文件名的方法
  • python 获取文件列表(或是目录例表)
  • Python实现查询某个目录下修改时间最新的文件示例


  • 上一条:
    深入浅析Python中join 和 split详解(推荐)
    下一条:
    全面了解Python环境配置及项目建立
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客