简体   繁体   中英

Python .exe script closes instantly in Windows

I have code that makes a backup of the files (their path in test.txt). It works from PyCharm and works fine. I used pyinstaller to compile it into an EXE. On my pc the exe runs and works fine, but on the other one it instantly closes Without profit. (Windows 10 1607 corp. 14393.4402 x64.). Input() at the end of the code does not help. I cannot instal python on machine, beacause havenot admin rights. Some ideas, please?

import os
import time
import zipfile

lines = []
with open('C:\\test.txt') as txt:
    lines = txt.read().splitlines()

reserve_list = lines


dir_to_save = 'C:\\test'

today = dir_to_save + os.sep + time.strftime('%Y-%m-%d')
now = time.strftime('%H-%M')

if not os.path.exists(today):
    os.mkdir(today)
    print('Dir created', today)

target = today + os.sep + now + '.zip'

filename = target

zip_obj = zipfile.ZipFile(filename, "w")

for file_dir in reserve_list:
    zip_obj.write(file_dir)
zip_obj.close()

input()

My exe dir after compile: 在此处输入图像描述

An executable file will close itself once it has successfully exited the containing program, so this is expected behaviour.

As for not being able to install python, check out a portable version of python (this one it for windows specifically).

Another option would be to run the executable through your command prompt, so in cmd you would type C:\Path\To\Executable.exe to execute it. When this executable exits, it will only exit the process in your command prompt but not the command prompt itself.

When you run it on another computer, does the program complete its task, or does it just close? If it just closes, it might be blocked by an antivirus.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM