[英]Py2app can't read files
I want to read from a file with a python application.我想从一个带有 python 应用程序的文件中读取。 Here is my source code.
这是我的源代码。
MyApplication.py
: MyApplication.py
:
if __name__ == '__main__':
fin = open("/absolute/path/to/file.txt", "r")
fin.readline()
fin.close()
setup.py
: setup.py
:
# Copied and pasted it from their docs
from setuptools import setup
setup(
app=["MyApplication.py"],
setup_requires=["py2app"],
)
Then I try to run python setup.py py2app
and everything goes smoothly, however when I open the app a popup appears saying "MyApplication Error".然后我尝试运行
python setup.py py2app
并且一切顺利,但是当我打开应用程序时会出现一个弹出窗口,上面写着“MyApplication Error”。 Is there a way for me to solve this or at least get more information about what's happening?有没有办法让我解决这个问题,或者至少获得有关正在发生的事情的更多信息?
EDIT FOR MORE CLARITY:编辑更清晰:
The line that is causing problems is fin.readline()
.导致问题的行是
fin.readline()
。 Also, writing to files works, and reading a file that the app itself has created doesn't generate errors.此外,写入文件有效,读取应用程序本身创建的文件不会产生错误。 This code, for example, works.
例如,此代码有效。
if __name__ == '__main__':
fout = open("/absolute/path/to/newfile.txt", "a+")
fout.write("Test\n")
fout.close()
fin = open("/absolute/path/to/newfile.txt", "r")
line = fin.readline()
fin.close()
fout = open("/absolute/path/to/newfile.txt", "a+")
fout.write("Line read: " + line)
fout.close()
The output file will show:输出文件将显示:
Test
Line read: Test
It was the encoding, because of course it was.这是编码,因为它当然是。 Just open the file like this.
只需像这样打开文件。
fin = open("/absolute/path/to/file.txt", "r", encoding="utf-8")
I found this hint veeery useful:我发现这个提示非常有用:
“right click -> Show Packages -> Resources -> Mac OS”, and execute your app directly do help, because it shows specific errors on the console
“右键-> Show Packages -> Resources -> Mac OS”,直接执行你的app做help,因为它会在控制台显示特定的错误
Source: http://uxcrepe.com来源: http : //uxcrepe.com
It might help you a lot, as it shows you typical python errors you are already familiar with!它可能对您有很大帮助,因为它向您展示了您已经熟悉的典型 Python 错误!
Enjoy!享受!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.