I have a directory with spectra in .txt files. And I want to read them all and process one spectrum per time using a for loop, so that I will not have to run the program every single time.
How can I do this?
I have a directory with spectra in .txt files. And I want to read them all and process one spectrum per time using a for loop, so that I will not have to run the program every single time.
How can I do this?
This will list all of the files in a directory with a given extension:
from os import listdir
def list_files(directory, extension):
return (f for f in listdir(directory) if f.endswith('.' + extension))
So you can then do:
files = list_files("/my/favourite/directory", "txt")
for f in files:
# Do something with f