0% found this document useful (0 votes)
21 views2 pages

Subprocess Module

The document explains how to use the subprocess module in Python to execute system commands, specifically the 'dir' command to list directory contents. It details the use of subprocess.Popen() for creating processes and capturing output and errors. Additionally, it introduces the mss module for taking screenshots, providing a simple example of capturing a screen shot and saving it as an image file.

Uploaded by

dipil67840
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)
21 views2 pages

Subprocess Module

The document explains how to use the subprocess module in Python to execute system commands, specifically the 'dir' command to list directory contents. It details the use of subprocess.Popen() for creating processes and capturing output and errors. Additionally, it introduces the mss module for taking screenshots, providing a simple example of capturing a screen shot and saving it as an image file.

Uploaded by

dipil67840
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/ 2

Subprocess module

import subprocess as sp
directory = input("enter directory : ") #Location of the directory where you
want to execute the command
process = sp.Popen(f"dir
{directory}",shell=True,stdout=sp.PIPE,stderr=sp.PIPE,text=True)
output,error=process.communicate()
print(f"Output \n {output}")
print(f"Error \n {error}")

Explanation:
1. import subprocess:

This imports the subprocess module, which is used to create and manage additional processes,
allowing interaction with the system's command line (or shell).

2. subprocess.Popen():
This is a more advanced way to execute system commands. It creates a new process and allows
interaction with it (e.g., sending input or reading output).
Arguments:
"dir":
The command to be executed. Here, dir is a Windows command that lists the contents of the
current directory.

shell=True:
Indicates that the command should be executed through the shell. This is necessary for certain
commands like dir on Windows.

stdout=subprocess.PIPE:
Redirects the standard output (the output of the command) to a pipe so it can be captured and
processed later.
stderr=subprocess.PIPE:

Redirects the standard error (error messages from the command) to a pipe for capture.
text=True:
Ensures the output is captured as a string (rather than bytes).
3. process.communicate():
This method waits for the command to complete and retrieves:

stdout: The captured standard output (command output).


stderr: The captured standard error (any error messages).

4. print("Output:", stdout):
Prints the output of the command (e.g., the directory listing).

If there is no error, stderr will be empty.

5. print("Errors:", stderr):
Prints any errors generated by the command. For example, if the command is invalid, this will
capture the error message.

mss module (screenshot)


>> pip install mss

import mss
with mss.mss() as screen:
screen = screen.shot(output="image1.png")

You might also like