• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python列出一个文件夹及其子目录的所有文件

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

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python 创建文件夹,python 遍历文件夹,python如何创建文件夹,python 删除文件夹,python读取文件夹等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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']
</div>

也可以用 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
</div>

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

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

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

import os
os.path.isfile('test.txt') #如果不存在就返回False
os.path.exists(directory) #如果目录不存在就返回False
</div>

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

</div>

您可能想查找下面的文章:

  • Python遍历文件夹和读写文件的实现方法
  • Python实现Windows和Linux之间互相传输文件(文件夹)的方法
  • python 实现删除文件或文件夹实例详解
  • Python文件与文件夹常见基本操作总结
  • python之文件的读写和文件目录以及文件夹的操作实现代码
  • Python遍历文件夹和读写文件的实现代码
  • 动感网页相册 python编写简单文件夹内图片浏览工具
  • python之文件的读写和文件目录以及文件夹的操作实现代码
  • Python遍历文件夹和读写文件的实现代码
  • 动感网页相册 python编写简单文件夹内图片浏览工具

相关文章

  • python实现简单的TCP代理服务器
  • Python中字符串格式化str.format的详细介绍
  • 使用Python简单的实现树莓派的WEB控制
  • Python实现爬取逐浪小说的方法
  • 在漏洞利用Python代码真的很爽
  • python中异常捕获方法详解
  • 对于Python编程中一些重用与缩减的建议
  • python下os模块强大的重命名方法renames详解
  • 仅用500行Python代码实现一个英文解析器的教程
  • python通过ElementTree操作XML获取结点读取属性美化XML

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python通过BF算法实现关键词匹配的方法
    • 用Python实现一个简单的能够发送带附件的邮件程序的教程
    • Pycharm学习教程(3) 代码运行调试
    • 用Python实现QQ游戏大家来找茬辅助工具
    • Windows下安装python MySQLdb遇到的问题及解决方法
    • Python多线程学习资料
    • python判断端口是否打开的实现代码
    • Python实现文件按照日期命名的方法
    • python使用PyGame绘制图像并保存为图片文件的方法
    • python集合类型用法分析

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有