別のテキスト ファイルに含まれるリストを処理しようとしています。ファイルを読み取り、ファイルの内容を含む出力を生成することはできますが、生成された出力は入力と同じ未処理のバージョンです。作業しているモジュール内にリストが含まれている場合、この問題は発生しません。プロセスは正常に実行されます。入力ファイルを使用すると問題が発生する理由はありますか?
これは正常に実行されます (注意してください - 両方の実行で、「正しい」は同じモジュール内の関数です)。
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()
これはしません:
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()
問題が何であるかについて何か考えはありますか?