The secret of Python operating CMD! Easily play with command line control

Introduction:

Command Line Interface (CLI) is a text-based user interface in a computer operating system that interacts with the computer by entering commands. As a powerful programming language, Python provides a wealth of libraries and modules that can easily operate CMD. This article will introduce in detail how to use Python to operate CMD to help you easily control the command line.

1. The os module in Python

The os module is a standard library in Python that provides an interface for interacting with the operating system. Through the os module, we can execute CMD commands, get the current directory, create and delete folders and other operations.

Execute CMD command

The os module in Python provides the function os.system(command) for executing CMD commands, where command is the CMD command to be executed. For example, to execute the ping command in CMD, you can use the following code:

import os``# Execute CMD command``os.system("ping www.example.com")

Get current directory

Use the os.getcwd() function of the os module to get the directory where the current Python script is located. For example, to get the current directory and print the output, you can use the following code:

import os`` ``# Get the current directory ``current_dir = os.getcwd()`` ``# Print the current directory ``print("Current directory:", current_dir)

Create and delete folders

Use the os.mkdir(path) function of the os module to create a folder, and the os.rmdir(path) function to delete a folder. where path is the path to the folder to be created or deleted. For example, to create a folder named “test” in the current directory, you can use the following code:

import os`` ``# Create folder ``os.mkdir("test")

2. Subprocess module in Python

The subprocess module is a standard library in Python for creating new processes, connecting to and communicating with subprocesses. Through the subprocess module, we can operate CMD more flexibly, including obtaining the output of CMD commands, writing input to CMD, etc.

Get the output of CMD command

Use the subprocess.run(args, capture_output=True, text=True) function of the subprocess module to execute the CMD command and return the output of the command. Among them, args is the CMD command to be executed, capture_output=True means capturing the output of the command, and text=True means returning the output result in text form. For example, to get the output of executing the “dir” command in CMD, you can use the following code:

import subprocess``# Execute the CMD command and get the output``result = subprocess.run(["dir"], capture_output=True, text=True)``# Print the output result``print(result.stdout)

Write input to CMD

Use subprocess.run(args, input=input_data, capture_output=True, text=True) of the subprocess module to execute the CMD command and write input to CMD. Among them, args is the CMD command to be executed, input=input_data indicates the input data to be written, capture_output=True indicates the output of the capture command, and text=True indicates that the output result is returned in text form. For example, to execute the “ping” command into CMD and write the input “www.example.com”, you can use the following code:

import subprocess``# Execute the CMD command and write the input ``result = subprocess.run(["ping"], input="www.example.com", capture_output=True, text=True)``# Print the output result ``print(result.stdout)

3. shutil module in Python

The shutil module is a standard library in Python for operating on files and folders. Through the shutil module, we can easily copy, move, and delete files and folders.

Copy files and folders

Files or folders can be copied using the shutil.copy(src, dst) function of the shutil module, where src is the source file or source folder path to be copied, and dst is the target file or destination folder path. For example, to copy the “example.txt” file in the current directory to the “test” folder, you can use the following code:

import shutil``# Copy file ``shutil.copy("example.txt", "test")

Move files and folders

Files or folders can be moved using the shutil.move(src, dst) function of the shutil module, where src is the source file or source folder path to be moved, and dst is the target file or destination folder path. For example, to move the “example.txt” file in the current directory to the “test” folder, you can use the following code:

import shutil``# Move files ``shutil.move("example.txt", "test")

Delete files and folders

Use the shutil module’s shutil.rmtree(path) function to delete a folder and all its contents, and the os.remove(path) function to delete files. For example, to delete the “example.txt” file and “test” folder in the current directory, you can use the following code:

import os``import shutil`` ``# Delete the file ``os.remove("example.txt")`` ``# Delete the folder and all its contents ``shutil.rmtree("test")

Conclusion:

By operating CMD in Python, we can easily execute commands, obtain output, create, copy, move and delete files and folders and other operations. Using modules such as os, subprocess and shutil, we can easily play with command line control and improve work efficiency. I hope this article can be helpful to you and help you better use Python for CMD operations.

Recommended learning resources

In addition to the above sharing, if you also like programming and want to get a higher salary by learning Python, here is a Python learning material to share with you.

Here I will show you the part-time job group I joined and the screenshots of recent orders I received.

Part-time group

Private Order

If friends are in need, you can click the link below to get it or Scan the QR code below to get it, or you can recommend to the part-time group~

CSDN spree, when the QR code expires, click here to receive it:[Collection of learning materials &Related tools&How to obtain PyCharm permanent version]< font color="#66cc66">

Learning Python well is good whether you are getting a job or doing a side job to make money, but you still need to have a learning plan to learn Python. Finally, we share a complete set of Python learning materials to give some help to those who want to learn Python!

1. Python learning route

image-20230619144606466

python learning roadmap 1

2. Basic learning of Python
1. Development tools

2. Study notes

3. Learning video

3. Essential manual for Python beginners

image

4. A complete set of data analysis resources

5. Python interview highlights
1. Interview materials

2. Resume template

CSDN spree, when the QR code expires, click here to receive it:[Collection of learning materials &Related tools&How to obtain PyCharm permanent version]< font color="#66cc66">


Due to limited space, only part of the information is shown. Add it above to get it




—— ?♂? This article is reproduced from the Internet. If there is any infringement, please contact us to delete it?♂? ——< /font>
syntaxbug.com © 2021 All Rights Reserved.