无论其扩展名如何获取具有相同名称的文件 [英] Getting files with same name irrespective of their extension
问题描述
我想列出所有相同名称的文件,而不管它们的扩展名。
os.walk
结果在空列表,当我尝试搜索没有扩展名的文件名,但是当我提到扩展名的文件名。它列出了所有目录中的文件名和扩展名。
def get_all_files(path):
数据文件= []
用于root,dirs,os.walk(路径)中的文件:
用于文件中的文件:
pathname = os.path.join(root,文件)
Datafiles.append([file,pathname])
返回数据文件
$ b
import os,fnmatch
$ b $ def get_all_files(path,pattern):
datafiles = []
用于root,dirs,os.walk(path)中的文件:
用于fnmatch.filter(文件,模式)中的文件:
pathname = os.path.join(root,file )
filesize = os.stat(pathname).st_size
datafiles.append([file,pathname,filesize])
return datafiles
print get_all_files( 。','something。*')#所有名为'something'的文件
可能还有更多的代码行,也可以使得更多的泛型支持所有的 os.walk()
的关键字参数:
import os,fnmatch
$ b def glob_walk(top,pattern,** kwargs):
在fnmatch模块中记载的os.walk()过滤文件的包装程序返回
,其模式由Unix shell风格的通配符
组成。
for root,dirs,os.walk中的文件(顶部,** kwargs):
产生root,dirs,fnmatch.filter(文件,模式)
#示例用法
def get_all_files(path,pattern):
for root,dirs,glob_walk(path,pattern)中的文件:
文件中的文件:
路径名= os.path.join(根,文件)
filesize = os.stat(路径名).st_size
产生文件,路径名,文件大小
打印列表(get_all_files(' 。','something。*'))#所有名为'something'的文件
在这个版本中, glob_walk()
函数(就像 get_all_files()
)是生成器,就像 os.walk()
。
I want to list down all the files with same name irrespective of their extensions.
os.walk
results in empty list when I try to search for the file name without extension but when i mention the file name with extension. It list down all the files present in any directory with that same name and extension.
def get_all_files(path):
Datafiles=[]
for root,dirs,files in os.walk(path):
for file in files:
pathname=os.path.join(root,file)
Datafiles.append([file,pathname])
return Datafiles
You can use the fnmatch.filter()
function to recognize the filenames of interest:
import os, fnmatch
def get_all_files(path, pattern):
datafiles = []
for root,dirs,files in os.walk(path):
for file in fnmatch.filter(files, pattern):
pathname = os.path.join(root, file)
filesize = os.stat(pathname).st_size
datafiles.append([file, pathname, filesize])
return datafiles
print get_all_files('.', 'something.*') # all files named 'something'
Note, however, it would be possible, with a few more lines of code, to also make something more generic that supported all of os.walk()
's keyword arguments:
import os, fnmatch
def glob_walk(top, pattern, **kwargs):
""" Wrapper for os.walk() that filters the files returned
with a pattern composed of Unix shell-style wildcards
as documented in the fnmatch module.
"""
for root, dirs, files in os.walk(top, **kwargs):
yield root, dirs, fnmatch.filter(files, pattern)
# sample usage
def get_all_files(path, pattern):
for root, dirs, files in glob_walk(path, pattern):
for file in files:
pathname = os.path.join(root, file)
filesize = os.stat(pathname).st_size
yield file, pathname, filesize
print list(get_all_files('.', 'something.*')) # all files named 'something'
Note that the new glob_walk()
function (as wll as get_all_files()
) in this version are generators, just like os.walk()
.
这篇关于无论其扩展名如何获取具有相同名称的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!