Reading files in a directory and saving each dynamically

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

doc = nlp(text)

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?


>Solution :

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 =

    doc = nlp(text)

Would it help?

Leave a Reply Cancel reply