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 !!!
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.")