-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()
是否該文件夾「出/」存在嗎?你在寫第一種情況的文件時是怎麼寫的? –
是的,文件夾'out /'存在。我正在將LaTeX代碼寫入文件。 –
如果我'mkdir out',然後在python 3 shell中執行'>>> problems = open(「out/problems.tex」,「w」,encoding =「utf8」)',我得到一個零長度的文件。其餘的代碼是否在執行某些操作來刪除文件? –