Where does a file gets stored that came with a .exe file on Windows 10/11?

Question:

So I have made this python file which I want to compile (it’s called ElPatron) to a .exe file with Nuitka.
I did it correctly and this is the dist folder that came with that (using the –standalone argument that Nuitka has)

This is the nuitka command I used:

.python.exe -m nuitka --mingw64 .ElPatron.py --standalone --onefile --windows-disable-console --windows-icon-from-ico=pdf.ico --include-data-file=C:UsersUserAppDataLocalProgramsPythonPython39example.pdf=example.pdf

I included the pdf in the exe using the –include-data-file argument from Nuitka

Dist folder that nuitka has created

The problem I have now is that I don’t know where this(the example.pdf) get’s stored when the ElPatron.exe is executed. I want to know this so I can call it inside my Python project.

The question

Where does the example.pdf get stored when a windows computer executes the .exe (containing the pdf)?

Asked By: Coder

||

Answers:

From looking at the Nuitka documentation, it says:

# Create a binary that unpacks into a temporary folder
python -m nuitka --onefile program.py

Note

There are more platform specific options, e.g. related to icons,
splash screen, and version information, consider the --help output
for the details of these and check the section “Good Looks”.

Again, on Windows, for the temporary file directory, by default the
user one is used, however this is overridable with a path
specification given in
--windows-onefile-tempdir-spec=%TEMP%\onefile_%PID%_%TIME% which is
the default and asserts that the temporary directories created cannot
collide.

So, I think the output should show up as a subdirectory in %TEMP% as described, which should be the path C:Users%USERNAME%AppDataLocalTemp.

If you want to use the default path, you’ll have to find the dynamically-named subfolder (onefile_%PID%_%TIME%) within %TEMP% in Python, and you can get the current process ID, then use a fuzzy pattern or something, as you won’t be able to know the exact time. Otherwise, set a custom path.

Answered By: mbomb007

A janky way of doing this might be:

def resource_path(self,relative_path):
    """ Get absolute path to resource, works for python file and compiled exe """
    if not sys.argv[0].endswith('exe'): return os.path.join(os.path.dirname(sys.argv[0]),relative_path)
    else: 
        if not hasattr(self,'respth'):
            temp_appd=os.path.join(os.getenv('localappdata'),'Temp')
            resdict={}
            for item in os.listdir(temp_appd):
                if len(re.findall('''onefile_d+_d+''',item))>0:
                    resdict[re.findall('''onefile_d+_(d+)''',item)[0]]=item
            self.respth=resdict[max(list(resdict.keys()))]
            logging.info('path set to '+os.path.join(os.getenv('localappdata'),'Temp',self.respth,relative_path))
        return os.path.join(os.getenv('localappdata'),'Temp',self.respth,relative_path)
Answered By: kwtf
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.