如何在列表中携带python目录中每个文件的名称?
How to carry in a list the name each file from a directory with python?
我正在将一些正则表达式应用于一个充满 .txt 文件的文件夹,以便提取一些特定的模式,如下所示:
def retrive(directory, a_regex):
for filename in glob.glob(os.path.join(directory, '*.txt')):
with open(filename, 'r') as file:
important_stuff = re.findall(a_regex, file.read(), re.S)
my_list = [tuple([j.split()[0] for j in i]) for i in important_stuff]
print my_list
lists_per_file = retrive(directory,regex_)
并且输出是列表中所有文件的所需内容:
[interesting stuff 1]
[interesting stuff 2]
[interesting stuff 3]
...
[interesting stuff n]
[interesting stuff n-1]
如何将每个文档文件的名称携带或绑定到列表,即类似这样的内容:
[interesting stuff 1], name_of_document_1
[interesting stuff 2], name_of_document_2
[interesting stuff 3],name_of_document_3
...
[interesting stuff n], name_of_document_n
[interesting stuff n-1], name_of_document_n-1
在此先感谢大家。
如果你想打印列表,然后打印文件名,两者之间没有换行符,你首先必须把列表变成一个字符串,然后去掉列表周围的括号。之后,您可以从您拥有的文件路径中获取文件名,并将两者放在一起。
见下方代码;
def retrive(directory, a_regex):
for filename in glob.glob(os.path.join(directory, '*.txt')):
with open(filename, 'r') as file:
important_stuff = re.findall(a_regex, file.read(), re.S)
my_list = [tuple([j.split()[0] for j in i]) for i in important_stuff]
# print my_list # old line
print str(my_list).strip('[]') + filename.split('/')[-1]
lists_per_file = retrive(directory,regex_)
我正在将一些正则表达式应用于一个充满 .txt 文件的文件夹,以便提取一些特定的模式,如下所示:
def retrive(directory, a_regex):
for filename in glob.glob(os.path.join(directory, '*.txt')):
with open(filename, 'r') as file:
important_stuff = re.findall(a_regex, file.read(), re.S)
my_list = [tuple([j.split()[0] for j in i]) for i in important_stuff]
print my_list
lists_per_file = retrive(directory,regex_)
并且输出是列表中所有文件的所需内容:
[interesting stuff 1]
[interesting stuff 2]
[interesting stuff 3]
...
[interesting stuff n]
[interesting stuff n-1]
如何将每个文档文件的名称携带或绑定到列表,即类似这样的内容:
[interesting stuff 1], name_of_document_1
[interesting stuff 2], name_of_document_2
[interesting stuff 3],name_of_document_3
...
[interesting stuff n], name_of_document_n
[interesting stuff n-1], name_of_document_n-1
在此先感谢大家。
如果你想打印列表,然后打印文件名,两者之间没有换行符,你首先必须把列表变成一个字符串,然后去掉列表周围的括号。之后,您可以从您拥有的文件路径中获取文件名,并将两者放在一起。
见下方代码;
def retrive(directory, a_regex):
for filename in glob.glob(os.path.join(directory, '*.txt')):
with open(filename, 'r') as file:
important_stuff = re.findall(a_regex, file.read(), re.S)
my_list = [tuple([j.split()[0] for j in i]) for i in important_stuff]
# print my_list # old line
print str(my_list).strip('[]') + filename.split('/')[-1]
lists_per_file = retrive(directory,regex_)