Edit multiple text files, and save as new files

Question:

My first post on StackOverflow, so please be nice. In other words, a super beginner to Python.

So I want to read multiple files from a folder, divide the text and save the output as a new file. I currently have figured out this part of the code, but it only works on one file at a time. I have tried googling but can’t figure out a way to use this code on multiple text files in a folder and save it as "output" + a number, for each file in the folder. Is this something that’s doable?

with open("file_path") as fReader:
 corpus = fReader.read()
loc = corpus.find("nn")
print(corpus[:loc], file=open("output.txt","a")) 
Asked By: Jonas

||

Answers:

Possibly work with a list, like:

from pathlib import Path

source_dir = Path("./") # path to the directory
files = list(x for x in filePath.iterdir() if x.is_file())

for i in range(len(files)):
    file = Path(files[i])
    outfile = "output_" + str(i) + file.suffix
    with open(file) as fReader, open(outfile, "w") as fOut:
        corpus = fReader.read()
        loc = corpus.find("nn")
        fOut.write(corpus[:loc])
    

** sorry for multiple editting….

Answered By: Sunsheep

welcome to the site. Yes, what you are asking above is completely doable and you are on the right track. You will need to do a little research/practice with the os module which is highly useful when working with files. The two commands that you will want to research a bit are:

os.path.join()
os.listdir()

I would suggest you put two folders within your python file, one called data and the other called output to catch the results. Start and see if you can just make the code to list all the files in your data directory, and just keep building that loop. Something like this should list all the files:

# folder file lister/test writer

import os

source_folder_name = 'data'   # the folder to be read that is in the SAME directory as this file
output_folder_name = 'output' # will be used later...

files = os.listdir(source_folder_name)

# get this working first
for f in files:
    print(f)


# make output folder names and just write a 1-liner into each file...
for f in files:
    output_filename = f.split('.')[0]   # the part before the period
    output_filename += '_output.csv'
    output_path = os.path.join(output_folder_name, output_filename)
    with open(output_path, 'w') as writer:
        writer.write('some data')
Answered By: AirSquid
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.