I want create text file in current directory
i wrote this code and it worked in vscode:
import os
p = __file__
p = str(p)
f = open(p + '.txt', 'a')
f.write('hello world')
But it does not create a text file when I use pyinstaller !!!
Take a look at the code below. You need to specify the correct file permissions when using the open() function.
import os
p = __file__
p = str(p)
try: # in the case that the file does not exist, you can use a try block to catch the error.
f = open(p + '.txt', 'w') # a works as well instead of w.
with f:
f.write('hello world')
except IOError:
print("Failed to open file.")
with to open and close files, also the file has to be closedwith open() as file so that the closing and errors is handled automatically or sth, ok this works but you don't need to then close the file yourselfpyinstaller on the OP's code and run it and see if it creates the file? (btw I didn't downvote)
aalso creates a new file if one doesn't exist already.exeand it worked fine, what exactly is the issue? are there any errors (run fromcmdto check)?