简体   繁体   English

为什么我的脚本在调用.EXE后停止执行命令?

[英]Why does my script stop executing commands after calling an .EXE?

Here is the relevant code from a Python script where a few commands are executed to copy an executable file and then execute it: 以下是Python脚本中的相关代码,其中执行一些命令来复制可执行文件然后执行它:

exe_file_path = os.getcwd() + r'\name_of_executable.exe'
temp_loc = os.environ['temp']

subprocess.Popen(r'copy %s %s' % (exe_file_path, temp_loc), shell=True)
exe_file_path = os.environ['temp'] + r'\name_of_executable.exe'
subprocess.Popen(r'start %s' % (exe_file_path), shell=True)
subprocess.Popen(r'del %s' % (exe_file_path), shell=True)

Currently, name_of_executable.exe only prints out text and then calls system("pause") . 目前, name_of_executable.exe只打印出文本,然后调用system("pause")

After the pause is executed, I push enter and I would assume the executable would close and the Python script would continue, but the last line of Python doesn't execute. 暂停执行后,我按enter ,我会假设可执行文件将关闭,Python脚本将继续,但Python的最后一行不会执行。

Is this because I'm using the TEMP folder? 这是因为我正在使用TEMP文件夹吗? (I'm executing from a command prompt running as administrator. How do I get the script to work? (我正在以管理员身份运行的命令提示符执行。如何让脚本运行?

All programs will be immediately started one after another. 所有程序将一个接一个地立即启动。 Call communicate on each Popen object to wait for program termination. 调用每个Popen对象进行通信以等待程序终止。

Additionally, your use of format strings is unnecessarily dangerous. 此外,您使用格式字符串是不必要的危险。 ['copy', exe_file_path, temp_loc] automatically escapes any strange characters in exe_file_path and temp_loc (and is easier to read). ['copy', exe_file_path, temp_loc]自动转义exe_file_pathtemp_loc任何奇怪字符(并且更容易阅读)。

By the way, Python has very good functions for copying and deleting files in shutil and os ; 顺便说一下,Python在shutilos中复制和删除文件有很好的功能; there is no need to call shell programs for that. 没有必要为此调用shell程序。

And instead of concatenating strings to determine exe_file_path , you should use os.path.join (although this is not that important, since your program seems locked to Windows). 而不是连接字符串来确定exe_file_path ,你应该使用os.path.join (虽然这不重要,因为你的程序似乎锁定到Windows)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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