import subprocess
# Define the path to your batch file
batch_file_path = "C:\\path\\to\\your\\script.bat"
# Run the batch file
try:
# Using subprocess.run() for simpler cases, waiting for completion
# capture_output=True captures stdout and stderr
# text=True decodes output as text
result = subprocess.run([batch_file_path], capture_output=True, text=True, check=True,
shell=True)
print("Batch file executed successfully.")
print("Standard Output:\n", result.stdout)
print("Standard Error:\n", result.stderr)
except subprocess.CalledProcessError as e:
print(f"Error executing batch file: {e}")
print("Standard Output:\n", e.stdout)
print("Standard Error:\n", e.stderr)
except FileNotFoundError:
print(f"Error: Batch file not found at {batch_file_path}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
# If you need to run the batch file in the background without waiting for completion,
# you can use subprocess.Popen()
# process = subprocess.Popen([batch_file_path], shell=True)
# print(f"Batch file started with PID: {process.pid}")