> new_file = open('filename.txt', 'w') > params = open('eggs.txt', 'r') > do all the python stuff here > new_file.close() > > If these files followed a naming convention such as 1.txt and 2.txt I > can easily see how these could be parsed consecutively in a loop. > However, they are not and so is it possible to modify this code such > that I can tell python to parse all .txt files in a certain directory > and then to save them as separate files? For instance, using the example > above, python would parse both spam.txt and eggs.txt and then save 2 > different files, say as spam_parsed.txt and eggs_parsed.txt.
import os for fname in os.listdir('.'): if not fname.lower().endswith('.txt'): continue new_file_name = '%s_parsed%s' % tuple( new_file = open(new_file_name, 'w') params = open(fname) # do all the python stuff here params.close() new_file.close() -tkc -- http://mail.python.org/mailman/listinfo/python-list