首页 > 编程知识 正文

python模块导入,python安装glob

时间:2023-05-06 04:59:19 阅读:155824 作者:3235

如何在Python中使用glob.glob模块搜索子文件夹

在文件夹中打开一系列子文件夹,希望找到文本文件并打印文本文件中的行。 我用这个:

configfiles=glob.glob (c :/users/Sam/desktop/file1/*.txt ) )

但是,也无权访问子文件夹。 有人知道如何使用同一命令访问子文件夹吗?

userymyasked 2019-06-12t 08:0:02 z

9个解决方案

105体积

在Python 3.5或更高版本中使用新的递归. txt功能:

configfiles=glob.glob (c :/users/Sam/desktop/file1/* *.txt ),recursive=True ) ) ) ) ) )

如果设置为. txt (fnmatch.filter ) ),则后跟路径分隔符以匹配零个或多个子目录。

在Python的早期版本中, txt无法递归列出子目录中的文件。

在这种情况下,请组合使用. txt和. txt。

import os

导入fnmatch

path=' c :/users/Sam/desktop/file1'

configfiles=[ OS.path.join (dirpath,f )

for dirpath,dirnames,files in os.walk(path )

forfinfnmatch.filter(files,' *.txt ' ) ]

这将递归遍历目录,并返回与. txt文件匹配的所有绝对路径名。 在这些特定情况下,您可以使用fnmatch.filter ()测试(.endswith ),虽然fnmatch.filter ()可能有点错误。

import os

path=' c :/users/Sam/desktop/file1'

configfiles=[ OS.path.join (dirpath,f )

for dirpath,dirnames,files in os.walk(path )

forfinfilesiff.Endswith('.txt ' ) ]

martijnpietersanswered 2019-06-12t 0833600336044 z

15 votes

glob2包支持通配符,速度相当快

code=' ' ' '

导入博客2

glob2.glob('files/*/** ' ) )

“”'

timeit.timeit(code,number=1) )。

在我的笔记本电脑上,&; gt; 60,000个文件路径。

megawacanswered 2019-06-12t 08:04:17 z

12 votes

要在直接子目录中查找文件:

configfiles=glob.glob (r ' c : (users (Sam (desktop (* (*.txt ) ) )

对于遍历所有子目录的递归版本,可以使用pathlib从Python 3.5传递recursive=True。

configfiles=glob.glob (r ' c : (users (Sam ) desktop(*(*.txt ) ) ) () ) ) )。

两个函数调用返回列表。 您可以使用pathlib一次返回一个路径。 或使用pathlib :

from pathlib import Path

path=path(r(c:(users ) Sam(desktop ) ) ) ) ) ) ) ) ) ) )。

txt _ files _ only _ subdirs=path.glob (' *.txt ' ) ) ) ) ) ) ) ) ) )0) ) 0

txt _ files _ all _ recursively=path.r glob (' *.txt ' ) # including the current dir

这两种方法都会返回迭代器。 可以一次获取一个路径。

jfs answered 2019-06-12t 08:05336004 z

6 votes

可以在Python 2.6中使用Formic

导入格式

fileset=formic.fileset (include=' * *.txt ',directory=' c :/users/Sam/desktop/' )

公开-我是这个包的作者。

andrewalcockanswered 2019-06-12t 08336005336038 z

2 votes

这是一个使用glob.glob功能而不是fnmatch的改编版本。

deffind_files(directory,pattern='*”) :

ifnotOS.path.exists(directory ) :

raisevalueerror (' directory not found { } '.format ) directory ) )

matches=[]

for root,dirnames,filenamesinOS.walk,directory,

for filename in filenames:

full_path=os.path.join(root,filename )

iffnmatch.filter([full_path],pattern ) :

Matches.append(OS.path.join ) root,filename ) )

返回匹配

如果你有以下的dir结构的话

tests/files

a0

() ) a0.txt

() ) a0.yaml

() b0

() ) b0.yaml

() b00.yaml

( a1

你可以做这样的事

files=utils.find _ files (' tests/files ',' **/b0/b*.yaml ' ) )

['tests/files/a0/b0/b0.yaml ',' tests/files/a0/b0/b00.yaml']

相当于fnmatch模式不仅与文件名匹配,还与整个文件名匹配。

cevarisanswered 2019-06-12t 08336006336022 z

2 votes

configfiles=glob.glob (c :/users/Sam/desktop/* */*.txt ) ) ) ) ) ) ) ) ) ) ) )。

使用glob2,而不是应用于所有情况

configfiles=glob2. glob (c :/users/Sam/desktop/*.*.txt ) ) ) ) ) ) ) ) ) ) ) )。

nileshkumaranswered 2019-06-12t 08336006336055 z

2 votes

如果你能安装glob2软件包.

导入博客2

filenames=glob2. glob (c : (top _ directory (* (*.ext ) ) whereextisaspecificfileeextension

folders=glob2. glob (c :\ top _ directory(* )' ) )

所有文件名和文件夹:

all _ ff=glob2. glob (c : ((top _ directory ) (* ) (* ) (* ) ) )

deab answered 2019-06-12t 08:07336029 z

1 votes

如果运行的是Python 3.4,则可以使用Path模块。 Path方法支持Path模式。 这意味着“此目录和所有子目录,递归”。 返回为所有匹配文件生成Path个对象的生成器。

from pathlib import Path

configfiles=path (c :/users/Sam/desktop/file1/' ).glob ) */*.txt ) )。

eugeneyarmashanswered 2019-06-12t 08336007336056 z

0 votes

正如Martijn指出的那样,glob只能通过Python 3.5中引入的configfiles算子来实现。 由于OP明确要求使用glob模块,因此以下内容返回行为类似的惯性评估迭代器

import os,glob,itertools

configfiles=ITER tools.chain.from _ iterable (glob.I glob (OS.path.join (root,' *.txt ' ) )

for root、dirs、file sinos.walk (c :/users/Sam/desktop/file1/' ) )

请注意,此方法只能使用configfiles重复一次。 如果需要可用于多个操作的实际配置文件列表,则必须使用list(configfiles )明确创建。

fxx answered 2019-06-12t 08:08336032 z

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。