简体   繁体   中英

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:

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

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.

Is this because I'm using the TEMP folder? (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.

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

By the way, Python has very good functions for copying and deleting files in shutil and os ; there is no need to call shell programs for that.

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

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