简体   繁体   中英

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. How can i read all these files into 1 single text file using python.

There is a function named list dir in the os module. this function returns a list of all files in a given directory. Then you can access each file using a for a loop. This tutorial will be helpful for listdir function. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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