What is system command in Python
A system command in Python is a command that is executed by the operating system, rather than by the Python interpreter. These commands are typically used to perform tasks such as manipulating files, listing directory contents, and controlling system processes.
Call a system command
You can execute a system command in Python using the ‘subprocess’ module. The ‘subprocess’ module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.
The most commonly used function in the ‘subprocess’ module is ‘subprocess.call()’. The ‘call()’ function runs the command specified in the first argument and waits for it to complete before continuing execution of the Python script.
Here’s an example of how you can use ‘subprocess.call()’ to execute the command “ls -l” (which lists the files and directories in a directory in long format):
import subprocess subprocess.call(["ls", "-l"])
You can also use subprocess.run() which is a more recent version of subprocess.call() , it also waits for the command to complete and returns a CompletedProcess class instance.
import subprocess result = subprocess.run(["ls", "-l"])
You can also use ‘subprocess.Popen()’ to create a new process and keep the Python script running. This is useful if you want to run a command and then later interact with its input/output/error pipes or wait for it to complete.
import subprocess p = subprocess.Popen(["ls", "-l"])
Please be cautious when executing command from user input as there is a security risk of command injection.
File Manipulation of system command in Python
Python provides several ways to interact with the file system and execute system commands. Here are some of the commonly used modules and functions for file manipulation and system commands:
os‘ module: This module provides a way to interact with the operating system. You can use the ‘
os‘ module to perform file-related operations such as creating, deleting, renaming, and listing files and directories.
import os # check if a file exists if os.path.exists("myfile.txt"): print("File exists") # create a new directory os.mkdir("mydir") # remove a file os.remove("myfile.txt") # list all files in a directory print(os.listdir("mydir"))
subprocess‘ module: This module provides a way to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.
import subprocess # run a system command and capture its output output = subprocess.check_output("ls -l", shell=True) # print the output print(output.decode())
shutil‘ module: This module provides a way to perform high-level file operations such as copying, moving, and archiving files and directories.
import shutil # copy a file to a new location shutil.copy("myfile.txt", "mydir/myfile.txt") # move a file to a new location shutil.move("myfile.txt", "mydir/myfile.txt") # archive a directory shutil.make_archive("mydir", "zip", "mydir")
These are just a few examples of the many ways you can manipulate files and execute system commands in Python. You can refer to the Python documentation for more details on these modules and functions.