如何在python中从多个文件中读取文本
我在一个文件夹中有很多文本文件,大约3000个文件,在每个文件中,第193行是唯一包含重要信息的行。如何使用python将所有这些文件读入一个文本文件。您需要列出目录并按如下方式读取每个文件:如何在python中从多个文件中读取文本,python,filesystems,Python,Filesystems,我在一个文件夹中有很多文本文件,大约3000个文件,在每个文件中,第193行是唯一包含重要信息的行。如何使用python将所有这些文件读入一个文本文件。您需要列出目录并按如下方式读取每个文件: import os for filename in os.listdir("/path/to/folder"): with open(f"/path/to/folder/{filename}", "r") as f: d
import os
for filename in os.listdir("/path/to/folder"):
with open(f"/path/to/folder/{filename}", "r") as f:
desired_line = f.readlines()[193]
... # Do whatever you need with the line
您可以在文件路径中循环,从每个文件中读取文本,然后在操作系统中使用行分隔符拆分行
import os
file_paths = [f"/path/to/file{i}.ext" for i in range(3000)]
info = []
for p in file_paths:
with open(p, "r") as file:
text = file.read(p)
line_list = text.split(os.linesep)
info.append(line_list[192])
你可以试试,如果你想要的话:
导入操作系统
重要的_行=[]
对于os.listdir(“数据”)中的文本文件:
text_from_file=open(os.path.join('data',textfile)).readlines()
如果len(文件中的文本)>=192:
重要提示行=文件[192]中的文本
重要行。追加(重要行)
#文件中第192行的列表
操作系统模块中有一个名为list dir的函数。此函数返回给定目录中所有文件的列表。然后可以使用for循环访问每个文件。
本教程将有助于listdir函数
下面是示例代码
import os
# your file path
path = ""
# to store the files or you can give this direct to the for loop like below
# for file in os.listdir(path)
dir_files = []
dir_files = os.listdir(path)
# to store your important text files.
# this list stores your important text line (193th line) in each file
texts = []
for file in dir_files:
with open(path + '\\' + file) as f:
# this t varialble store your 192th line in each cycle
t = f.read().split('\n')[192]
# this file append each file into texts list
texts.append(t)
# to print each important line
for text in texts:
print(text)
将文件作为字符串读取,然后按
linesep
将其拆分,这与file.readlines()
基本相同。而且,使用列表理解来获得这样的文件列表是不可靠的,因为不是每个文件都有相同的名称,也不是只有3000个文件。最好的方法是使用os.listdir(“/path/to/folder”)
获取文件名我使用列表理解只是为了创建一个可运行的示例。我不知道readlines()
。。今天学到了一些东西:)这是随机读取文件的。是否有机会按照添加的时间顺序读取?我可以使用类似的东西吗?其中files
是文件列表:升序:sorted\u by\u mtime\u升序=sorted(文件,key=lambda t:os.stat(t).st\u mtime)
降序:sorted\u by\u mtime\u降序=sorted(文件,key=lambda t:-os.stat(t).st\u mtime)