【发布时间】:2017-04-25 16:23:01
【问题描述】:
settings.txt 在已编译的单文件应用程序中存储和访问,但不会被写入。当文件与脚本位于同一目录中时,这在 Pyinstaller 编译之前有效。
应用程序是从终端编译的:
pyinstaller script.spec script.py --windowed --onefile
a.datas 在规范文件中设置为:
a.datas += [(‘settings.txt’,’/path/to/settings.txt’, "DATA”)]
并且文件在应用程序中被正确读取:
with open(resource_path('settings.txt'), 'r') as f2
但是,当试图覆盖文件时,文件没有更新:
def OnExit(self, event):
with open(resource_path('settings.txt'), 'w') as f2:
f2.write('update')
self.Destroy()
resource_path 定义为:
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.environ.get("_MEIPASS2", os.path.abspath("."))
return os.path.join(base_path, relative_path)
【问题讨论】:
-
f2.write('update')如果不关闭文件,您将丢失数据。退出应用前需要f2.close()。 -
相信使用
with时会自动调用close()方法@ -
如果您的设置文件为空,则否!
-
另一点,您如何知道退出时“一切都已完成”?退出前需要验证一切。
-
它正在编写它们...但它不会重新捆绑您的应用程序,因此下次运行它时它将与原始应用程序一起扩展您的捆绑包...尝试将您的设置文件保存在 @987654331 之类的地方@
标签: python pyinstaller