如何改进Python脚本避免Application.exe失败时挂起及处理弹窗问题?
Hey there! Let's tackle your three main issues one by one and give you an improved script that fixes all of them:
import subprocess import sys application = r'.\Application.exe' input_params = ['-i', 'Input_1', '-j', 'Input_2'] # Use list instead of string for safety & reliability # Configure startup info to hide the application window (Windows-only) startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW startupinfo.wShowWindow = subprocess.SW_HIDE # Alternative for Python 3.7+: Use CREATE_NO_WINDOW for simpler window suppression # startupinfo.dwFlags |= subprocess.CREATE_NO_WINDOW try: # Use subprocess.run for streamlined process management & error handling result = subprocess.run( [application] + input_params, shell=False, # Avoid shell=True to eliminate security risks and parsing issues stderr=subprocess.STDOUT, stdout=subprocess.PIPE, text=True, # Returns output as string (instead of bytes) for readability startupinfo=startupinfo, check=True # Triggers exception if app exits with non-zero code ) # Access successful execution output if needed print("Application ran successfully. Output:", result.stdout) except subprocess.CalledProcessError as e: print(f"Application failed with exit code {e.returncode}: {e.stdout}", file=sys.stderr) sys.exit(e.returncode) # Terminate script immediately with the same error code
Key Improvements Explained:
Stop Infinite Hanging & Auto-Terminate on Failure
subprocess.run()replacesPopen+communicate()for simpler, more reliable process handling—it automatically waits for the app to finish, so you don't have to manage process handles manually.- When
check=Trueis set, the script will catch non-zero exit codes and trigger an exception. We handle this by printing the error and exiting immediately withsys.exit(), so the script won't hang waiting for manual input.
Trigger Exceptions on Application Failure
- The
check=Trueflag is exactly what you need here. IfApplication.exereturns any non-zero exit code (like your 255), it will throw asubprocess.CalledProcessError—no more silent failures.
- The
Hide Popup Windows & Skip Manual Closure
- For Windows systems, we use
startupinfoto configure the app to run hidden:STARTF_USESHOWWINDOWtells Windows we want to control window visibility.SW_HIDEensures the app window never appears. For newer Python versions,CREATE_NO_WINDOWis a more direct way to disable the window entirely.
- We removed
shell=Truebecause it's unnecessary here and can introduce security vulnerabilities. Passing arguments as a list avoids parsing errors and shell injection risks.
- For Windows systems, we use
For Older Python Versions (Pre-3.5):
If you can't use subprocess.run(), adjust the script to use Popen with manual error checking:
p = subprocess.Popen( [application] + input_params, shell=False, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, startupinfo=startupinfo ) stdout, _ = p.communicate() if p.returncode != 0: print(f"Application failed with exit code {p.returncode}: {stdout.decode()}", file=sys.stderr) sys.exit(p.returncode)
内容的提问来源于stack exchange,提问作者JimmyBuffet_Express
相关产品推荐
相关产品推荐

