Evgeny Mamaev
Evgeny Mamaev

Reputation: 1367

Pyinstaller create a data file at runtime

I created a .exe file with Pyinstaller. The program should create a data file with the results at runtime. It works in the Python interpreter mode when I execute python test.py but it doesn't work in the .exe mode. It simply doesn't create a file and responds with an error.

Which flags should I add to the command python -m PyInstaller --hidden-import=timeit --hidden-import=bisect -F test.py to make it work?

The exception with this setup is:

Error: [Errno 2] No such file or directory: "C:\Users\Admin\AppData\Local\Temp\..."

Where the aforementioned directory is temporary and I don't have access to it.

The piece of code which is supposed to write a file is:

def write_file(data, path, encoding='utf-8'):
'''Creates report files.'''
try:
    if config.python_version == 2 and type(data) in (list, str):
        f = io.open(path, 'wb') 
    else: 
        f = io.open(path, 'w', encoding=encoding, newline='')
    if type(data) is list:
        writer = csv.writer(f)
        writer.writerows(data)
    else:
        f.write(data)
    f.close()
    console(u'Report file: ' + path)
except IOError as e:
    console(e, level=Level.error)

I assume there should be a setting to point to the place where the file should be saved.

I've checked here https://pyinstaller.readthedocs.io/en/stable/spec-files.html#adding-files-to-the-bundle but without success. I couldn't use the listed flags properly, nothing seemed to work.

How can I specify the place where the file would be saved?

Upvotes: 1

Views: 1807

Answers (1)

Mars
Mars

Reputation: 2572

The issue isn't with pyinstaller, it's with however you're creating your file.
You may be using some environment variable when running your python script from the command line that isn't set when you run your Exe


I have created a simple example of a program that creates a data file in the directory from which it is called:

#myscript.py
f = open("Test.txt", "w")
print("Hello World!", file=f)

Then I generate the Exe with Pyinstaller:

pyinstaller -F myscript.py

Copy the exe anywhere and you can create Test.txt, if you have permissions in that folder.

Double click myscript.exe
Test.txt appears in the same folder as myscript.exe

Hello World!

Upvotes: 2

Related Questions