简体   繁体   English

如何从python中的多个文件中读取文本

[英]how to read text from multiple files in python

I have many text files around 3000 files in a folder and in each file the 193rd line is the only line which has important information.我在一个文件夹中有大约 3000 个文件的许多文本文件,在每个文件中,第 193 行是唯一包含重要信息的行。 How can i read all these files into 1 single text file using python.如何使用 python 将所有这些文件读入 1 个文本文件。

There is a function named list dir in the os module. os 模块中有一个名为 list dir 的函数。 this function returns a list of all files in a given directory.此函数返回给定目录中所有文件的列表。 Then you can access each file using a for a loop.然后您可以使用 for 循环访问每个文件。 This tutorial will be helpful for listdir function.本教程将对 listdir 功能有所帮助。 https://www.geeksforgeeks.org/python-os-listdir-method/ https://www.geeksforgeeks.org/python-os-listdir-method/

The example code is below.示例代码如下。

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)

You will need to list the directory and read each file like so:您需要列出目录并读取每个文件,如下所示:

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

You can loop through your file paths, read the text from every file and then split lines using the line seperator in your OS.您可以遍历文件路径,从每个文件中读取文本,然后使用操作系统中的行分隔符拆分行。

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])

You could try that, if its what you want:你可以试试,如果它是你想要的:

import os
important_lines = []
for textfile in os.listdir('data'):
    text_from_file = open( os.path.join('data', textfile) ).readlines()
    if len(text_from_file) >= 192:
        important_line = text_from_file[192]
        important_lines.append(important_line) 
        #list of 192th lines from files

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM