Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reading files in a directory and saving each dynamically

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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 = text_open.read()

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

Would it help?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading