我已输入从Numbers.txt文件,并希望写out.txt文件输出, 任何人都可以引导什么错误。无法获取文本文件输出在Python 2.7
import num2word_EN as s
text = open("C:\\Users\\eanaaks\\Desktop\\Python Practice Program\\Numbers.txt","r")
outfile = open("C:\\Users\\eanaaks\\Desktop\\Python Practice Program\\out.txt", "w")
for line in text:
line = line.rstrip()
num = int(line)
print line
x = s.to_card(num)
print (x)
outfile.write("%s\n"%(line));
outfile.close()
text.close()
请问你的文件是什么样子? –
你需要缩进'out_file.write()'否则你会只写最后一行 –
你没有做'text.close()',看看这个例子:http://stackoverflow.com/问题/ 4617034/how-can-i-open-multiple-files-using-open-in-python这使得它更容易编写错误免费。我也不会在for循环中导入某些东西。 – martijnn2008