Pyinstaller accesses txt files but does not write them (works until compiling the application)

settings.txt is stored and processed in a compiled single-phase application, but is not written. This works before compiling Pyinstaller when the file is in the same directory as the script.

The application is compiled from the terminal:

pyinstaller script.spec script.py --windowed --onefile

a.datas installed in the specification file as:

a.datas += [('settings.txt','/path/to/settings.txt', "DATA")]

and the file is read correctly in the application:

 with open(resource_path('settings.txt'), 'r') as f2 

However, the file is not updated when trying to overwrite the file:

 def OnExit(self, event): with open(resource_path('settings.txt'), 'w') as f2: f2.write('update') self.Destroy() 

resource_path 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) 
0
python pyinstaller
Dec 10 '16 at 7:14
source share
1 answer

If you are on Windows, _MEIPASS returns a "short" name for the path if any of its components is longer than 8 characters. So, to verify that this is a problem, try making this one-folder frozen application, and then move it in a simple and short way: for example, C:/test .

If this is a problem, you can solve this problem by extracting a long way using something like:

 if hasattr(sys, '_MEIPASS'): import win32api sys_meipass = win32api.GetLongPathName(sys._MEIPASS) 
+1
Dec 11 '16 at 15:55
source share



All Articles