settings.txt is stored and accessed within the compiled single file app, but it's not being written to. This works prior to Pyinstaller compilation when the file is in the same directory as the script.
The app is compiled from the terminal:
pyinstaller script.spec script.py --windowed --onefile
a.datas
is set in the spec file as:
a.datas += [(‘settings.txt’,’/path/to/settings.txt’, "DATA”)]
and the file is read properly within the app:
with open(resource_path('settings.txt'), 'r') as f2
However, the file isn’t updated when attempting to overwrite the file:
def OnExit(self, event):
with open(resource_path('settings.txt'), 'w') as f2:
f2.write('update')
self.Destroy()
resource_path
is defined as:
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)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…