Python脚本中的可执行文件无法写入文件

问题描述 投票:0回答:1

我有一个python代码,以文本形式将一些输出写入定义的输出文件。在PyCharm中,我的脚本可以正常运行,但是当我使用pyinstaller将xx.py转换为.exe文件时,exe文件无法写入已经位于同一路径的输出文件。

要编写输出,我使用此代码:

fg = open('input.src')
output_file = open("output.obj", "w")
for line in fg:
**do something**
output_file = open("output.obj", "a")
output_file.write("[" + str(hex(memory_location)) + "]" + "  " + output + "\n")

我还尝试了不带PyCharm的xx.py,带有控制台,脚本运行良好。

python-3.x pycharm
1个回答
0
投票

当我将其转换为.exe文件时...

不清楚您指的是什么转换过程。

绝对PyCharm正在将标准python解释器作为子进程运行。在您遇到问题的情况下,听起来您可能正在运行cython或其他异常语言环境。

您写道:

output_file = open("output.obj", "w")
...
output_file = open("output.obj", "a")

您选择不使用with习惯用语。更重要的是,您选择在重新打开之前不选择output_file.close()。您的语言解释器+文件系统的组合可能需要先关闭再进行后续打开。


0
投票

感谢@mypetlion,此问题是由输入文件的ANSI编码引起的。它需要更改为UTF-8。

© www.soinside.com 2019 - 2024. All rights reserved.