Python不会创建新文件

时间:2014-03-19 00:00:33

标签: python python-3.x

我正在使用Python 3.4。我有一个女巫的程序我有两个文件:

problems = open("out/problems.tex", 'w')
answers = open("out/answers.tex", 'w')

如果我运行此操作,我会收到错误:

Traceback (most recent call last):
  File "<encoding error>", line 10, in <module>
  File "C:\Python33\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 247-251:   character maps to <undefined>

如果我这样做:

problems = open("out/problems.tex", 'w', encoding='utf8')
answers = open("out/answers.tex", 'w', encoding='utf8')

我没有错误,但我也没有文件'problem.tex'和'answers.tex'。有谁知道我做错了什么?

这是我的完整计划:

from polygen import *

head_file = open("tex/head.tex", 'r', encoding="utf8")
ground_file = open("tex/ground.tex", 'r', encoding="utf8")

problems = open("out/problems.tex", 'w', encoding="utf8")
answers = open("out/answers.tex", 'w', encoding="utf8")

head = head_file.read()
problems.write(head)
answers.write(head)

t = polymult(7, 1, 2, 2, 3, 4)

problems.write(t[0])
answers.write(t[1])

ground = ground_file.read()
problems.write(ground)
answers.write(ground)

problems.close()
answers.close()

1 个答案:

答案 0 :(得分:0)

太奇怪了。 2意见建议:

  1. 使用绝对路径,确保生成文件。
  2. 如果源文件中有非ASCii字符,请使用“rb”/“wb”模式代替“r”/“w”