PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, August 13, 2022

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

 August 13, 2022     exe, output, pyinstaller, python     No comments   

Issue

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.



Answered By - AKX
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing