I have a Python script that looks like
path = os.path.join(os.path.dirname(sys.executable), 'data.txt')
file = open(path, 'w')
file.write("something")
file.close()
When I use Pyinstaller (with the option --onefile
), and open the resulting .exe-file, it does not seem to do anything. In particular, I cannot find a data.txt file. How do I fix this?
>Solution :
os.path.dirname(sys.executable)
would point at the directory where Python.exe is if you were not running under Pyinstaller, and it’s unlikely you’d want to write there. It’s likely that under PyInstaller, it’s some temporary directory.
Instead, just 'data.txt'
(or os.path.join(os.getcwd(), 'data.txt')
if you want to be pedantic) would create the file in the program’s current working directory, which, if you just double-click on the EXE, would be the EXE’s directory.