Python检查目录中具有特定名称的所有文件

标签 python list file glob

我有一个文件名 (mytest.),带有 3 个或更多后缀 (".txt",".shp",".shx", ".dbf") 在目录中 (path = 'C://sample')。我希望列出所有具有相同名称的文件

我知道使用 import glob 可以列出所有具有特定后缀的文件

我的问题是如何列出目录中的所有“mytest”

ex 

mytest.txt
mytest.shp
mytest.bdf
mytest.shx

mylist = ["mytest.txt","mytest.shp","mytest.bdf","mytest.shx"]


mytest.txt
mytest.shp
mytest.bdf
mytest.shx
mytest.pjr

mylist = ["mytest.txt","mytest.shp","mytest.bdf","mytest.shx","mytest.pjr"]

最佳答案

您正在寻找 glob module ,正如您在问题中所说,但在扩展名中使用了通配符:

import glob
glob.glob("mytest.*")

例子:

$ ls
a.doc  a.pdf  a.txt  b.doc

$ python
Python 2.7.3 (default, Dec 18 2012, 13:50:09) [...]
>>> import glob
>>> glob.glob("a.*")
['a.doc', 'a.pdf', 'a.txt']
>>>

关于Python检查目录中具有特定名称的所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22154818/

相关文章:

python - 正确理解 pexpect 的异步

java 8 UnsupportedOperationException on subList.clear()

python - 在单行列表理解中操作字典列表

c++ - 如何从列表中删除一个元素?

c++ - 使用 EOF 函数作为条件

php - 将txt文件转换为excel,然后修改excel并将新的excel文件导入数据库

c++ - 一次又一次尝试在给出错误路径的同时打开文件

python - 如何对挂起的 Paho Python Mqtt Single Publish 进行故障排除

python - 如何防止 setuptools 安装包作为 .egg

python - 当我在 Python 中解码字节(HTML)时缺少代码(请求、BeautifulSoup、urllib)