I'm trying to process a list contained within a separate text file. I am able to read the file, and generate an output containing the contents of the file, but the output generated is the unprocessed version, identical to the input. I do not encounter this problem when the list is contained within the module that I'm working with - the process runs successfully. Any ideas why using the input file is causing problems?
This runs successfully (please note - for both runs, "correct" is a function within the same module):
import sys
sys.stdout = open('out.txt','w')
def spelltest():
for i in test:
print correct(i)
test = ['acess','accesing','accomodation','acommodation','acomodation',
'acount','adress','adres','addresable','aranged','arrainged',
'arrangeing','aranging','arragment','articals',
'annt','anut','arnt','auxillary','avaible',
'awfall','afful','basicaly','begining',
'benifit','benifits','beetween',
'bicycal','bycicle','bycycle']
if __name__ == '__main__':
print spelltest()
This does not:
import sys
sys.stdout = open(r'C:\Python26\out.txt','w')
infile = open('C:\\Python26\\text.txt','r')
def spelltest():
for line in infile:
print correct(line)
if __name__ == '__main__':
print spelltest()
The text file contains a list of the following: ['acess', 'accesing', 'accomodation', 'acommodation', 'acomodation', 'acount', 'adress', 'adres', 'addresable', 'aranged', 'arrainged']
Any thoughts on what the issue may be?