Search code examples
pythonwindowsvisual-studio-codenetshauto-py-to-exe

Script does not save output to correct path while using auto py to exe


my program uses netsh to save wifi profile information on a txt file in a directory that is named as the system time of the moment of running the program

so lets say you have signed into three wifi networks on your PC:

Wifi1: password:123456 wifi2: password: abcdefg wifi3: password: 12456!

and the py file path is:

C:\Users\User\Desktop\Pp

and the system time is:

2024-01-23 16;51;44.894633

the output should be three txt files saved to directory:

c:\Users\User\Desktop\Pp\2024-01-23 16;51;44.894633

this works when i just run the python program through VScode, but i wanted to be able to make it an executable with auto py to exe

the problem is that it saves the txt to a different directory ONLY when i use the exe file. so when i open the exe file the path will be three txt files saved to this path:

saved to C:\Users\User\AppData\Local\Temp_MEI124562\2024-01-23 16;51;44.894633

in short it doesnt save the files to the correct path ONLY when using the .exe file, but it works correctly when i run the .py file.

python code:

import os
from datetime import datetime
names_string = os.popen("netsh wlan show profile")

names_list = []
for i in names_string:
    if ":" in i:
        names_list.append(i[i.index(":") + 2:len(i) - 1])
    else:
        next

foldername = f"{datetime.now()}"
folder_name = foldername
folder_name = folder_name.replace(":", ";")
parent_path = os.path.dirname(__file__)
path = os.path.join(parent_path, folder_name)
os.mkdir(path)

for i in names_list:
    with open (path + f"/{i}.txt", 'w') as fp:
        print("saved to", path + f"/{i}.txt")
        output = os.popen(f"""netsh wlan show profile name = "{i}" key = clear""").read()
        fp.write(output)

input("press enter to close window")

auto py to exe output:

Running auto-py-to-exe v2.42.0
Building directory: C:\Users\User\AppData\Local\Temp\tmps_xeexcq
Provided command: pyinstaller --noconfirm --onefile --console  "C:/Users/User/Desktop/Pp/main.py"
Recursion Limit is set to 5000
Executing: pyinstaller --noconfirm --onefile --console C:/Users/User/Desktop/Pp/main.py --distpath C:\Users\User\AppData\Local\Temp\tmps_xeexcq\application --workpath C:\Users\User\AppData\Local\Temp\tmps_xeexcq\build --specpath C:\Users\User\AppData\Local\Temp\tmps_xeexcq

21259 INFO: PyInstaller: 6.3.0
21263 INFO: Python: 3.12.1
21302 INFO: Platform: Windows-11-10.0.22621-SP0
21316 INFO: wrote C:\Users\User\AppData\Local\Temp\tmps_xeexcq\main.spec
21332 INFO: Extending PYTHONPATH with paths
['C:\\Users\\User\\Desktop\\Pp']
21955 INFO: checking Analysis
21969 INFO: Building Analysis because Analysis-00.toc is non existent
21984 INFO: Initializing module dependency graph...
22003 INFO: Caching module graph hooks...
22083 INFO: Analyzing base_library.zip ...
23851 INFO: Loading module hook 'hook-heapq.py' from 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\PyInstaller\\hooks'...
23871 INFO: Loading module hook 'hook-encodings.py' from 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\PyInstaller\\hooks'...
27590 INFO: Loading module hook 'hook-pickle.py' from 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\PyInstaller\\hooks'...
31066 INFO: Caching module dependency graph...
31219 INFO: Running Analysis Analysis-00.toc
31223 INFO: Looking for Python shared library...
31243 INFO: Using Python shared library: C:\Users\User\AppData\Local\Programs\Python\Python312\python312.dll
31250 INFO: Analyzing C:\Users\User\Desktop\Pp\main.py
31270 INFO: Processing module hooks...
31282 INFO: Performing binary vs. data reclassification (2 entries)
31302 INFO: Looking for ctypes DLLs
31315 INFO: Analyzing run-time hooks ...
31338 INFO: Including run-time hook 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_inspect.py'
31363 INFO: Looking for dynamic libraries
31552 INFO: Extra DLL search directories (AddDllDirectory): []
31558 INFO: Extra DLL search directories (PATH): []
31870 WARNING: Library not found: could not resolve 'api-ms-win-core-path-l1-1-0.dll', dependency of 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python312\\python312.dll'.
31887 INFO: Warnings written to C:\Users\User\AppData\Local\Temp\tmps_xeexcq\build\main\warn-main.txt
31919 INFO: Graph cross-reference written to C:\Users\User\AppData\Local\Temp\tmps_xeexcq\build\main\xref-main.html
31990 INFO: checking PYZ
31994 INFO: Building PYZ because PYZ-00.toc is non existent
32005 INFO: Building PYZ (ZlibArchive) C:\Users\User\AppData\Local\Temp\tmps_xeexcq\build\main\PYZ-00.pyz
32311 INFO: Building PYZ (ZlibArchive) C:\Users\User\AppData\Local\Temp\tmps_xeexcq\build\main\PYZ-00.pyz completed successfully.
32336 INFO: checking PKG
32351 INFO: Building PKG because PKG-00.toc is non existent
32355 INFO: Building PKG (CArchive) main.pkg
34491 INFO: Building PKG (CArchive) main.pkg completed successfully.
34521 INFO: Bootloader C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\PyInstaller\bootloader\Windows-64bit-intel\run.exe
34528 INFO: checking EXE
34537 INFO: Building EXE because EXE-00.toc is non existent
34550 INFO: Building EXE from EXE-00.toc
34566 INFO: Copying bootloader EXE to C:\Users\User\AppData\Local\Temp\tmps_xeexcq\application\main.exe
34754 INFO: Copying icon to EXE
34865 INFO: Copying 0 resources to EXE
34884 INFO: Embedding manifest in EXE
35011 INFO: Appending PKG archive to EXE
35036 INFO: Fixing EXE headers
38225 INFO: Building EXE from EXE-00.toc completed successfully.

Moving project to: C:\Users\User\Desktop\Pp\output
Complete.

i tried to reinstall auto py to exe multipul times, i tried disabling windows defender (as it has a pop up every time i use auto py to exe) and my antivirus, tried on two different pcs just for it to not work, i tried reinstalling python twice.and i havent found any useful information online.

thanks in advance!


Solution

  • Try this:

    import os
    from datetime import datetime
    import sys
    
    exe_path = os.path.abspath(sys.argv[0])
    exe_dir = os.path.dirname(exe_path)
    
    
    names_string = os.popen("netsh wlan show profile")
    
    names_list = []
    for i in names_string:
        if ":" in i:
            names_list.append(i[i.index(":") + 2:len(i) - 1])
        else:
            next
    
    foldername = f"{datetime.now()}"
    folder_name = foldername
    folder_name = folder_name.replace(":", ";")
    path = os.path.join(exe_dir, folder_name)
    os.mkdir(path)
    
    for i in names_list:
        with open (path + f"/{i}.txt", 'w') as fp:
            print("saved to", path + f"/{i}.txt")
            output = os.popen(f"""netsh wlan show profile name = "{i}" key = clear""").read()
            fp.write(output)
    
    input("press enter to close window")