简体   繁体   English

Python glob 包括隐藏的文件和文件夹

[英]Python glob include hidden files and folders

I try to loop over all files matching a certain extension, including those inside hidden folders.我尝试遍历所有匹配特定扩展名的文件,包括隐藏文件夹中的文件。 So far I haven't found a way to do this with iglob .到目前为止,我还没有找到使用iglob执行此操作的方法。 This works for all folder except those starting with a dot:这适用于所有文件夹,但以点开头的文件夹除外:

import glob
for filename in glob.iglob('/path/**/*.ext', recursive=True):
    print(filename)

I have tried to add the dot as an optional character to no avail.我试图将点添加为可选字符但无济于事。 I'd really like to use glob instead of residing to os.walk .我真的很想使用glob而不是驻留在os.walk

How to include all files/folders, even those starting with .如何包含所有文件/文件夹,甚至是那些以. , with glob ? ,与glob

From https://docs.python.org/3/library/glob.html来自https://docs.python.org/3/library/glob.html

Note that unlike fnmatch.fnmatch(), glob treats filenames beginning with a dot (.) as special cases请注意,与 fnmatch.fnmatch() 不同,glob 将以点 (.) 开头的文件名视为特殊情况

If the directory contains files starting with .如果目录包含以 . they won't be matched by default.默认情况下它们不会匹配。 For example, consider a directory containing card.gif and .card.gif:例如,考虑一个包含 card.gif 和 .card.gif 的目录:

 import glob glob.glob('*.gif') # ['card.gif'] glob.glob('.c*') # ['.card.gif']

From what I see it requires two separate globs to get both hidden and not hidden ones, for example using https://stackoverflow.com/a/4829130/4130619 .从我看来,它需要两个单独的 glob 来获取隐藏和非隐藏的 glob,例如使用https://stackoverflow.com/a/4829130/4130619

I had this same issue and wished glob.glob had an optional parameter to include dot files.我有同样的问题,并希望 glob.glob 有一个可选参数来包含点文件。 I wanted to be able to include ALL dot files in ALL directories including directories that start with dot.我希望能够在所有目录中包含所有点文件,包括以点开头的目录。 Its just not possible to do this with glob.glob.使用 glob.glob 无法做到这一点。 However I found that Python has pathlib standard module which has a glob function which operates differently, it will include dot files.但是我发现 Python 有 pathlib 标准模块,它有一个操作不同的 glob 函数,它将包含点文件。 The function operates a little differently, in particular it does not return a list of strings, but instead path objects.该函数的操作略有不同,特别是它不返回字符串列表,而是返回路径对象。 However I used the following但是我使用了以下内容

files=[]
file_refs = pathlib.Path(".").glob(pattern)
for file in file_refs:
    files.append(str(file))

The other noticeable difference I found was a glob pattern ending with **.我发现的另一个显着差异是以 ** 结尾的全局模式。 This returned nothing in the pathlib version but would return all the files in the glob.glob one.这在 pathlib 版本中没有返回任何内容,但会返回 glob.glob 中的所有文件。 To get the same results I added a line to check if the pattern ended with ** and if so then append /* to it.为了获得相同的结果,我添加了一行来检查模式是否以 ** 结尾,如果是,则将 /* 附加到它。

The following code is a replacement for your example that include the files in directories starting with dot以下代码替换了您的示例,其中包含以点开头的目录中的文件

import pathlib
for fileref in pathlib.Path('/path/').glob('**/*.ext'):
    filename = str(fileref)
    print(filename)

Adding an answer for the bounty question;添加赏金问题的答案; getting the result of hidden and non-hidden files in a single command.在单个命令中获取隐藏和非隐藏文件的结果。

As @reducidng activity mentioned, glob treats .正如@reducidng 活动所提到的, glob 对待 . files as a special use-case.文件作为特殊用例。 To get both regular and hidden files in a single loop, we can use itertools.chain with glob.iglob iterators.要在单个循环中同时获取常规文件和隐藏文件,我们可以使用itertools.chainglob.iglob迭代器。 for example,例如,

→ ls -A
.chen     file.text so1.py

>>> import glob, itertools
>>> for i in itertools.chain(glob.iglob('**'), glob.iglob('.**')):
...     print(i)
...
file.text
so1.py
.chen

# If you want it as a variable, you can list() it.
>>> l = list(itertools.chain(glob.iglob('**'), glob.iglob('.**')))
>>> l
['file.text', 'so1.py', '.chen']
>>>

Note: it does not fully work (yet).注意:它不能完全工作(还)。 Let's say you have .hello , .dot/hello.txt , .dot/.hello.txt , nodot/hello.txt , nodot/.hello.txt .假设您有.hello.dot/hello.txt.dot/.hello.txtnodot/hello.txtnodot/.hello.txt Then neither:然后既不:

itertools.chain(glob.iglob('**', recursive=True), glob.iglob('.**', recursive=True))

nor也不

itertools.chain(glob.iglob('**/*', recursive=True), glob.iglob('.**/*', recursive=True))

give all files.给所有文件。

From python 3.11 onward it is possible to do:从 python 3.11 开始,可以执行以下操作:

glob.iglob('/path/*', include_hidden=True)

To find hidden files matching a certain extension, you can try this要查找与特定扩展名匹配的隐藏文件,您可以试试这个

glob.glob('/path/**/.*.ext')

If you want to find all files in a folder如果要查找文件夹中的所有文件

glob.glob('/path/*') + glob.glob('/path/.*')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM