如何从python中编写一个.py文件,使其类型不应该像“带有Windows CRLF的ASCII文件”
因为当我在windows中运行file.write(data)时,它会写文件,但当我尝试eval(open(file.py).read())时,它会失败,并给出语法错误,因为windows CRLF在每一行上......
请参阅错误日志-回溯
ERROR:web-services:[25]: info = eval(tools.file_open(terp_file).read(-1))
ERROR:web-services:[26]: File "<string>", line 1
ERROR:web-services:[27]: {
ERROR:web-services:[28]:发布于 2010-04-29 20:37:05
您可以:
open(file, 'wb')或...data.replace('\r','')的命令进行写入
我会避开exec,转而使用execfile as mentioned by SilentGhost。
发布于 2010-04-29 20:29:53
问题不在于CRLF,而在于eval用于计算单个表达式,而不是整个程序。
可以使用exec从字符串执行程序,也可以使用execfile直接从文件执行程序。
要回答您最初的问题,您可以通过以二进制模式打开文件来避免编写CRLF:f = open(filename, 'wb')
发布于 2010-04-29 20:30:45
我认为您正在寻找execfile函数。
https://stackoverflow.com/questions/2737208
复制相似问题