Reading files in a directory and saving each dynamically

Question:

text_open = open("inputfiles/(22).txt", "r")
text = text_open.read()

doc = nlp(text)
db.add(doc)
db.to_disk("./outputfiles/22.spacy")

I am trying to loop over each of the 500+ documents in the inputfiles folder and output them through db.to_disk. Instead of changing the hard coded numbers every-time, how would I dynamically rename each new output file to match the input file?

If I were to open the directory with glob/os, how do I then add this to the output directory without overwriting every single file with the hardcoded ’22’ or creating new 22(1).spacy, 22(2).spacy etc files?

Thanks!

Asked By: goonhoon

||

Answers:

If you’re using Python 3.6+, then use f-strings.

for i in range(1, 501):
    text_open = open(f"inputfiles/({i}).txt", "r")
    text = text_open.read()

    doc = nlp(text)
    db.add(doc)
    db.to_disk(f"./outputfiles/{i}.spacy")

Would it help?

Answered By: cup11
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.