Python:如何找到具有特定扩展名的所有文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3608411/
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
Python: How can I find all files with a particular extension?
提问by BeeBand
I am trying to find all the .cfiles in a directory using Python.
我正在尝试.c使用 Python查找目录中的所有文件。
I wrote this, but it is just returning me all files - not just .cfiles.
我写了这个,但它只是返回给我所有文件 - 不仅仅是.c文件。
import os
import re
results = []
for folder in gamefolders:
for f in os.listdir(folder):
if re.search('.c', f):
results += [f]
print results
How can I just get the .cfiles?
我怎样才能得到.c文件?
采纳答案by deif
try changing the inner loop to something like this
尝试将内部循环更改为这样的
results += [each for each in os.listdir(folder) if each.endswith('.c')]
回答by fredley
for _,_,filenames in os.walk(folder):
for file in filenames:
fileExt=os.path.splitext(file)[-1]
if fileExt == '.c':
results.append(file)
回答by Maciej Kucharz
Try "glob":
试试“glob”:
>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']
回答by Manoj Govindan
import os, re
cfile = re.compile("^.*?\.c$")
results = []
for name in os.listdir(directory):
if cfile.match(name):
results.append(name)
回答by Vatine
If you replace '.c'with '[.]c$', you're searching for files that contain .cas the last two characters of the name, rather than all files that contain a c, with at least one character before it.
如果替换'.c'为'[.]c$',则搜索包含.c名称最后两个字符的文件,而不是所有包含 a 的文件c,并且前面至少有一个字符。
Edit: Alternatively, match f[-2:]with '.c', this MAY be computationally cheaper than pulling out a regexp match.
编辑:另外,匹配f[-2:]带'.c',这可能是计算量比拉出一个正则表达式匹配便宜。
回答by Jive Dadson
KISS
吻
# KISS
import os
results = []
for folder in gamefolders:
for f in os.listdir(folder):
if f.endswith('.c'):
results.append(f)
print results
回答by volting
For another alternative you could use fnmatch
对于另一种选择,您可以使用fnmatch
import fnmatch
import os
results = []
for root, dirs, files in os.walk(path)
for _file in files:
if fnmatch.fnmatch(_file, '*.c'):
results.append(os.path.join(root, _file))
print results
or with a list comprehension:
或使用列表理解:
for root, dirs, files in os.walk(path)
[results.append(os.path.join(root, _file))\
for _file in files if \
fnmatch.fnmatch(_file, '*.c')]
or using filter:
或使用过滤器:
for root, dirs, files in os.walk(path):
[results.append(os.path.join(root, _file))\
for _file in fnmatch.filter(files, '*.c')]
回答by Scott
The implementation of shutil.copytree is in the docs. I mofdified it to take a list of extentions to INCLUDE.
Shutil.copytree 的实现在文档中。我修改了它以获取 INCLUDE 的扩展列表。
def my_copytree(src, dst, symlinks=False, *extentions):
""" I modified the 2.7 implementation of shutils.copytree
to take a list of extentions to INCLUDE, instead of an ignore list.
"""
names = os.listdir(src)
os.makedirs(dst)
errors = []
for name in names:
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if symlinks and os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
my_copytree(srcname, dstname, symlinks, *extentions)
else:
ext = os.path.splitext(srcname)[1]
if not ext in extentions:
# skip the file
continue
copy2(srcname, dstname)
# XXX What about devices, sockets etc.?
except (IOError, os.error), why:
errors.append((srcname, dstname, str(why)))
# catch the Error from the recursive copytree so that we can
# continue with other files
except Error, err:
errors.extend(err.args[0])
try:
copystat(src, dst)
# except WindowsError: # cant copy file access times on Windows
# pass
except OSError, why:
errors.extend((src, dst, str(why)))
if errors:
raise Error(errors)
Usage: For example, to copy only .config and .bat files....
用法:例如,仅复制 .config 和 .bat 文件....
my_copytree(source, targ, '.config', '.bat')
my_copytree(源代码,targ,'.config','.bat')
回答by tjborromeo
Just to be clear, if you wanted the dot character in your search term, you could've escaped it too:
需要明确的是,如果您想要搜索词中的点字符,您也可以将其转义:
'.*[backslash].c' would give you what you needed, plus you would need to use something like:
'.*[backslash].c' 会给你你需要的东西,另外你需要使用类似的东西:
results.append(f), instead of what you had listed as results += [f]
results.append(f), 而不是你列出的结果 += [f]
回答by firegurafiku
There is a better solution that directly using regular expressions, it is the standard library's module fnmatchfor dealing with file name patterns. (See also globmodule.)
有一个更好的解决方案是直接使用正则表达式,它是标准库fnmatch中处理文件名模式的模块。(另见glob模块。)
Write a helper function:
编写一个辅助函数:
import fnmatch
import os
def listdir(dirname, pattern="*"):
return fnmatch.filter(os.listdir(dirname), pattern)
and use it as follows:
并按如下方式使用它:
result = listdir("./sources", "*.c")

