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
Solution 1:
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:
withopen(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 linefor text in texts:
print(text)
Solution 2:
You will need to list the directory and read each file like so:
import os
for filename in os.listdir("/path/to/folder"):
withopen(f"/path/to/folder/{filename}", "r") as f:
desired_line = f.readlines()[193]
... # Do whatever you need with the line
Solution 3:
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 inrange(3000)]
info = []
for p in file_paths:
withopen(p, "r") as file:
text = file.read(p)
line_list = text.split(os.linesep)
info.append(line_list[192])
Solution 4:
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()
iflen(text_from_file) >= 192:
important_line = text_from_file[192]
important_lines.append(important_line)
#list of 192th lines from files
Post a Comment for "How To Read Text From Multiple Files In Python"