TheSartorsss
TheSartorsss

Reputation: 113

Py2app can't read files

I want to read from a file with a python application. Here is my source code.

MyApplication.py:

if __name__ == '__main__':
    fin = open("/absolute/path/to/file.txt", "r")
    fin.readline()
    fin.close()

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". 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(). 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

Upvotes: 0

Views: 647

Answers (2)

chrischma
chrischma

Reputation: 163

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

Source: http://uxcrepe.com

It might help you a lot, as it shows you typical python errors you are already familiar with!

Enjoy!

Upvotes: 1

TheSartorsss
TheSartorsss

Reputation: 113

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

Upvotes: 0

Related Questions