使用自动 py 执行时,脚本不会将输出保存到正确的路径

问题描述 投票:0回答:1

我的程序使用netsh将wifi配置文件信息保存在一个txt文件中,该文件位于一个名为运行程序时刻的系统时间的目录中

假设您已在 PC 上登录了三个 wifi 网络:

无线网络1: 密码:123456 无线网络2: 密码:abcdefg 无线网络3: 密码:12456!

py文件路径为:

C:\用户\用户\桌面\Pp

系统时间为:

2024-01-23 16;51;44.894633

输出应该是保存到目录中的三个txt文件:

c:\用户\用户\桌面\Pp�4-01-23 16;51;44.894633

当我只是通过 VScode 运行 python 程序时,这是有效的,但我希望能够使用 auto py to exe 使其成为可执行文件

问题是,仅当我使用 exe 文件时,它才会将 txt 保存到不同的目录。所以当我打开exe文件时,路径将是保存到此路径的三个txt文件:

保存到 C:\Users\User\AppData\Local\Temp_MEI124562�4-01-23 16;51;44.894633

简而言之,仅在使用 .exe 文件时它不会将文件保存到正确的路径,但当我运行 .py 文件时它可以正常工作。

Python代码:

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

自动 py 到 exe 输出:

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.

我尝试多次重新安装auto py to exe,我尝试禁用Windows Defender(因为每次我使用auto py to exe时它都会弹出)和我的防病毒软件,在两台不同的电脑上尝试过,但它不起作用,我尝试重新安装 python 两次。但我在网上没有找到任何有用的信息。

提前致谢!

python windows visual-studio-code netsh auto-py-to-exe
1个回答
0
投票

试试这个:

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")
© www.soinside.com 2019 - 2024. All rights reserved.