-1

How do I loop through a directory?I'm doing this to spit out predictions in tensorflow for poets.It usually takes in only a single file and spits out its prediction. I'm planning to give it the entire directory,and save predictions in some text file.Im doing this through the docker interface,im thinking of writing a script which will go through all the files in the directory one by one and store the predictions in some text file.

Any help would be appreciated.

.

Alex
  • 18,484
  • 8
  • 60
  • 80
  • 1
    Possible duplicate of [How can I iterate over files in a given directory?](https://stackoverflow.com/questions/10377998/how-can-i-iterate-over-files-in-a-given-directory) – Alex Jun 08 '17 at 14:04

1 Answers1

1

I faced a similar issue while writing a code. The following code will recurse through all the files in the folder(including subfolders as well) given by folder_name.

import os

with open('prediction.txt','w') as tf_file:
    for dirPath, dirNames, fileNames in os.walk(folder_name):
        for fileName in fileNames:
            results = perform_tensorflow_stuff(fileName)
            tf_file.writelines('\n'.join(results))

Hope this helps!

Arjun Balgovind
  • 596
  • 1
  • 5
  • 13