2014-03-19 61 views
-1

我正在使用Python 3.4。我在女巫一个节目,我有两个文件:Python不会创建新文件

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') 

我没有得到任何错误但我也没有得到任何文件'problems.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() 
+0

是否该文件夹“出/”存在吗?你在写第一种情况的文件时是怎么写的? –

+0

是的,文件夹'out /'存在。我正在将LaTeX代码写入文件。 –

+0

如果我'mkdir out',然后在python 3 shell中执行'>>> problems = open(“out/problems.tex”,“w”,encoding =“utf8”)',我得到一个零长度的文件。其余的代码是否在执行某些操作来删除文件? –

回答

0

太奇怪了。 2意见建议:

  1. 使用绝对路径,确保生成的文件。
  2. 使用“RB” /“白平衡”模式,而不是“R” /“W”如果你在源文件中的非ASCII字符
+0

谢谢。我发现非ASCII字符,现在它工作。 –