Python 使用通配符搜索文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3348753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:35:45  来源:igfitidea点击:

Search for a file using a wildcard

pythonfilewildcard

提问by Stan

I want get a list of filenames with a search pattern with a wildcard. Like:

我想获取带有通配符的搜索模式的文件名列表。喜欢:

getFilenames.py c:\PathToFolder\*
getFilenames.py c:\PathToFolder\FileType*.txt
getFilenames.py c:\PathToFolder\FileTypeA.txt

How can I do this?

我怎样才能做到这一点?

采纳答案by Martin

You can do it like this:

你可以这样做:

>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']

Note: If the directory contains files starting with .they won't be matched by default. For example, consider a directory containing card.gifand .card.gif:

注意:如果目录包含以.它们开头的文件,则默认情况下不会匹配。例如,考虑一个包含card.gif和的目录.card.gif

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

This comes straight from here: http://docs.python.org/library/glob.html

这直接来自这里:http: //docs.python.org/library/glob.html

回答by Daniel Egeberg

from glob import glob
import sys

files = glob(sys.argv[1])

回答by Donald Miner

globis useful if you are doing this in within python, however, your shell may not be passing in the *(I'm not familiar with the windows shell).

glob如果您在 python 中执行此操作,则很有用,但是,您的 shell 可能没有传入*(我不熟悉 windows shell)。

For example, when I do the following:

例如,当我执行以下操作时:

import sys
print sys.argv

On my shell, I type:

在我的 shell 上,我输入:

$ python test.py *.jpg

I get this:

我明白了:

['test.py', 'test.jpg', 'wasp.jpg']

Notice that argvdoes not contain "*.jpg"

注意argv不包含"*.jpg"

The important lesson here is that most shells will expand the asterisk at the shell, before it is passed to your application.

这里的重要教训是,在将星号传递给您的应用程序之前,大多数 shell 会扩展 shell 处的星号

In this case, to get the list of files, I would just do sys.argv[1:]. Alternatively, you could escape the *, so that python sees the literal *. Then, you can use the globmodule.

在这种情况下,要获取文件列表,我只需执行sys.argv[1:]. 或者,您可以转义*,以便 python 看到文字*. 然后,您可以使用该glob模块。

$ getFileNames.py "*.jpg"

or

或者

$ getFileNames.py \*.jpg

回答by Holt

I am adding this to the previous because I found this very useful when you want your scripts to work on multiple shell and with multiple parameters using *.

我将这个添加到前一个是因为我发现当你希望你的脚本在多个 shell 上工作并且使用多个参数时这非常有用*

If you want something that works on every shells, you can do the following (still using glob):

如果您想要一些适用于每个 shell 的东西,您可以执行以下操作(仍在使用glob):

>>> import glob
>>> from functools import reduce # if using python 3+
>>> reduce(lambda r, x: r + glob.glob(x), sys.argv[1:], [])

Note that it can produce duplicate (if you have a testfile and you give t*and te*), but you can simply remove them using a set:

请注意,它可能会产生重复项(如果您有一个test文件并且您提供t*te*),但您可以简单地使用 a 删除它们set

>>> set(reduce(lambda r, x: r + glob.glob(x), sys.argv[1:], []))