Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I include an output file when I convert a .py file to .exe using Pyinstaller?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading