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

作者:qizok 时间:2023-12-08 17:49:01 

python简介

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

Python由Guido van Rossum于 * 底发明,第一个公开发行版发行于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.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

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


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

以上所述是小编给大家介绍的Python列出一个文件夹及其子目录的所有文件网站的支持!

标签:python,文件夹,文件
0
投稿

猜你喜欢

  • Windows64x下VScode下载过程

    2021-03-08 20:10:47
  • python3中TQDM库安装及使用详解

    2023-02-17 20:05:55
  • python装饰器使用方法实例

    2022-12-14 11:24:07
  • SqlServer应用之sys.dm_os_waiting_tasks 引发的疑问(中)

    2024-01-16 02:25:00
  • Django实现自定义路由转换器

    2021-09-10 05:49:04
  • javascript计算星座属相(十二生肖属相)示例代码

    2024-05-02 17:24:11
  • css网页下拉菜单制作方法(1):基本原理

    2007-02-03 11:39:00
  • Python中处理时间的几种方法小结

    2021-03-15 17:58:33
  • python判断设备是否联网的方法

    2022-05-03 12:34:55
  • Python+Django搭建自己的blog网站

    2022-02-24 16:32:45
  • kNN算法python实现和简单数字识别的方法

    2023-09-05 21:44:36
  • 详解Python 实现元胞自动机中的生命游戏(Game of life)

    2023-05-31 07:11:54
  • CSS教程:关于网页图片的属性的介绍

    2008-10-31 12:02:00
  • vue-cli npm如何解决vue项目中缺失core-js的问题

    2024-04-28 09:30:26
  • php实现贪吃蛇小游戏

    2024-05-02 17:16:36
  • 任意定制文本对齐方式:CSS Text Wrapper

    2008-02-03 11:11:00
  • 如何使用Python多线程测试并发漏洞

    2021-11-18 08:25:52
  • 用css和js实现firefox和IE支持局部打印

    2008-05-23 13:04:00
  • python中执行smtplib失败的处理方法

    2023-03-31 17:41:35
  • 基于MSELoss()与CrossEntropyLoss()的区别详解

    2022-05-17 19:18:27
  • asp之家 网络编程 m.aspxhome.com