[go: up one dir, main page]

0% found this document useful (0 votes)
13 views1 page

Python Code For Batch File

This document is about the coding in python. Do you want to add a batch file in python script this is for you.

Uploaded by

IrfanShoaib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views1 page

Python Code For Batch File

This document is about the coding in python. Do you want to add a batch file in python script this is for you.

Uploaded by

IrfanShoaib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

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

You might also like