簡體   English   中英

Py2app 無法讀取文件

[英]Py2app can't read files

我想從一個帶有 python 應用程序的文件中讀取。 這是我的源代碼。

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

然后我嘗試運行python setup.py py2app並且一切順利,但是當我打開應用程序時會出現一個彈出窗口,上面寫着“MyApplication Error”。 有沒有辦法讓我解決這個問題,或者至少獲得有關正在發生的事情的更多信息?


編輯更清晰:

導致問題的行是fin.readline() 此外,寫入文件有效,讀取應用程序本身創建的文件不會產生錯誤。 例如,此代碼有效。

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

輸出文件將顯示:

Test
Line read: Test

這是編碼,因為它當然是。 只需像這樣打開文件。

fin = open("/absolute/path/to/file.txt", "r", encoding="utf-8")

我發現這個提示非常有用:

“右鍵-> Show Packages -> Resources -> Mac OS”,直接執行你的app做help,因為它會在控制台顯示特定的錯誤

來源: http : //uxcrepe.com

它可能對您有很大幫助,因為它向您展示了您已經熟悉的典型 Python 錯誤!

享受!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM