This question already has an answer here:
这个问题已经有了答案:
- Python Glob without the whole path - only the filename 3 answers
- Python通配符,没有完整的路径-只有文件名3
I am trying to make a list of all files in a directory with filenames in a that end in .root.
我正在尝试列出一个目录中的所有文件的列表,其中的文件名以.root结尾。
After reading some writings in the forum I tried to basic strategies using glob and os.listdir but I got into trouble for both of them
在论坛上阅读了一些文章后,我尝试了使用glob和os的基本策略。但是他们两个我都惹上麻烦了
First, when I use
首先,当我使用
import glob
filelist = glob.glob('/home/usr/dir/*.root')
It does make a list of string with all filenames that end in .root but I still face a problem.
它确实创建了一个字符串列表,所有文件名以.root结尾,但是我仍然面临一个问题。
I would like to be the list of string to have filenames as '/dir/.root' but the string has full path '/home/usr/dir/.root'
我想要成为有文件名的字符串列表。但字符串有完整路径'/home/usr/dir/.root'
Second, if I use os.listdir, I get into the trouble that
其次,如果我使用os。李斯特,我有麻烦了
path = '/home/usr/'
filelist = os.listdir(path + 'dir/*.root')
syntax error
which tells me that I can not only get the list of files for .root.
这告诉我,我不仅可以得到.root的文件列表。
In summary, I would like to make a list of filenames, that end in .root and are in my /home/usr/dir, while cutting off the '/home/usr' part. If I use globe, I get into the trouble of having /home/usr/. If I use os.listdir, I can't specify ".root" endling.
总之,我想列出一个文件名列表,它以.root结尾,在my /home/usr/dir中,同时删除“/home/usr”部分。如果我用地球仪,我就会有/home/usr/的麻烦。如果我使用操作系统。listdir,我不能指定"。“无尽的根源。
2 个解决方案
#1
2
glob
will return paths in a format matching your query, so that
glob将以与查询匹配的格式返回路径
glob.glob("/home/usr/dir/*.root")
# ['home/usr/dir/foo.root', 'home/usr/dir/bar.root', ...]
glob.glob("*.root")
# ['foo.root', 'bar.root', ...]
glob.glob("./*.root")
# ['./foo.root', './bar.root', ...]
...and so forth.
…等等。
To get only the filename, you can use path.basename
of the os
module, something like this:
要获取文件名,可以使用path。操作系统模块的basename:
from glob import glob
from os import path
pattern = "/home/usr/dir/*.root"
files = [path.basename(x) for x in glob(pattern)]
# ['foo.root', 'bar.root', ...]
...or, if you want to prepend the dir
part:
…或者,如果您想在dir部分之前:
pattern = "/home/usr/dir/*.root"
files = [path.join('dir', path.basename(x)) for x in glob(pattern)]
# ['dir/foo.root', 'dir/bar.root', ...]
...or, if you really want the path separator at the start:
…或者,如果您真的想在开始时使用路径分隔符:
from glob import glob
import os
pattern = "/home/usr/dir/*.root"
files = [os.sep + os.path.join('dir', os.path.basename(x)) for x in glob(pattern)]
# ['/dir/foo.root', '/dir/bar.root', ...]
Using path.join
and path.sep
will make sure that the correct path syntax is used, depending on your OS (i.e. /
or \
as a separator).
使用路径。加入和路径。sep将确保使用正确的路径语法,这取决于您的操作系统(即/或作为分隔符)。
Depending on what you are really trying to do here, you might want to look at os.path.relpath
, for the relative path. The title of your question indicates that relative paths might be what you are actually after:
根据您在这里真正要做的事情,您可能需要查看os.path。关系路径。你的问题的标题表明,相对路径可能是你真正想要的:
pattern = "/home/usr/dir/*.root"
files = [os.path.relpath(x) for x in glob(pattern)]
# files will now contain the relative path to each file, from the current working directory
#2
0
just use glob
for getting the list you want
and then use os.path.relpath
on each file
只需使用glob获取所需的列表,然后使用os.path。relpath在每个文件
import glob
files_names = []
for file in glob.glob('/home/usr/dir/*.root'):
files_names.append(os.path.relpath(file, "/home/usr"))
You can also use regex
您还可以使用regex
import re
files_names.append(re.sub(r'//home//usr//','', file, flags=re.I))
#1
2
glob
will return paths in a format matching your query, so that
glob将以与查询匹配的格式返回路径
glob.glob("/home/usr/dir/*.root")
# ['home/usr/dir/foo.root', 'home/usr/dir/bar.root', ...]
glob.glob("*.root")
# ['foo.root', 'bar.root', ...]
glob.glob("./*.root")
# ['./foo.root', './bar.root', ...]
...and so forth.
…等等。
To get only the filename, you can use path.basename
of the os
module, something like this:
要获取文件名,可以使用path。操作系统模块的basename:
from glob import glob
from os import path
pattern = "/home/usr/dir/*.root"
files = [path.basename(x) for x in glob(pattern)]
# ['foo.root', 'bar.root', ...]
...or, if you want to prepend the dir
part:
…或者,如果您想在dir部分之前:
pattern = "/home/usr/dir/*.root"
files = [path.join('dir', path.basename(x)) for x in glob(pattern)]
# ['dir/foo.root', 'dir/bar.root', ...]
...or, if you really want the path separator at the start:
…或者,如果您真的想在开始时使用路径分隔符:
from glob import glob
import os
pattern = "/home/usr/dir/*.root"
files = [os.sep + os.path.join('dir', os.path.basename(x)) for x in glob(pattern)]
# ['/dir/foo.root', '/dir/bar.root', ...]
Using path.join
and path.sep
will make sure that the correct path syntax is used, depending on your OS (i.e. /
or \
as a separator).
使用路径。加入和路径。sep将确保使用正确的路径语法,这取决于您的操作系统(即/或作为分隔符)。
Depending on what you are really trying to do here, you might want to look at os.path.relpath
, for the relative path. The title of your question indicates that relative paths might be what you are actually after:
根据您在这里真正要做的事情,您可能需要查看os.path。关系路径。你的问题的标题表明,相对路径可能是你真正想要的:
pattern = "/home/usr/dir/*.root"
files = [os.path.relpath(x) for x in glob(pattern)]
# files will now contain the relative path to each file, from the current working directory
#2
0
just use glob
for getting the list you want
and then use os.path.relpath
on each file
只需使用glob获取所需的列表,然后使用os.path。relpath在每个文件
import glob
files_names = []
for file in glob.glob('/home/usr/dir/*.root'):
files_names.append(os.path.relpath(file, "/home/usr"))
You can also use regex
您还可以使用regex
import re
files_names.append(re.sub(r'//home//usr//','', file, flags=re.I))