Python os.system() Method

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The os.system() method in Python is a powerful utility that allows you to execute shell commands directly from a Python script. This function bridges the Python environment and the underlying operating system, enabling you to interact with the command-line interface seamlessly.

Syntax of Python os.system() Method

The syntax for using the os.system() in Python:

To use the os.system() method in Python, we must import the os module. This module is part of the Python standard library and provides a suite of functions for interacting with the operating system.

Parameters of Python os.system() Method

The os.system() method takes a single argument:

  • command:
    This parameter specifies the shell command that you want to execute. It can be any valid command that your operating system supports.

Return Value of Python os.system() Method

The method, os.system in Python does not return any value. It simply executes the command and returns control to the Python script.

Exceptions of Python os.system() Method

ExceptionDescriptionCommon Causes
OSErrorRaised when the os.system in Python encounters an error related to the operating system.A non-existent command, Permission issues, and Other system-related problems
PermissionErrorRaised when attempting to execute a command that requires higher privileges or permissions than the current user possesses.Insufficient permissions to perform the requested action.
TypeErrorOccurs when the os.system() method is not used correctly, such as passing an argument of an incorrect data type.Providing a non-string type as an argument.
ValueErrorSimilar to TypeError, a ValueError can occur if the arguments provided to os.system() are invalid.Providing invalid or empty arguments.

How Does the Python os.system() Method Work?

The os.system in Python allows you to execute shell commands directly from your Python script.

  1. Command Execution:
    When you call os.system(command), Python hands over the command to the underlying operating system.
  2. Shell Environment:
    The command is executed within the shell environment, an interface for interacting with the operating system.
  3. Synchronous Execution:
    The os.system() method performs synchronous execution, which means it waits for the command to finish before proceeding.
  4. Output Handling:
    Any output generated by the command (such as standard output and error messages) is displayed in the terminal or console.
  5. Return Value:
    After the command completes execution, control is returned to your Python script. It returns an exit code.
  6. Exit Code:
    The exit code is a numeric value that indicates the command's status. A return value of 0 indicates successful execution, while non-zero values signify errors.

The os.system() method is available as a cross-platform method, but the syntax of the commands may vary between operating systems. For example, to list files in a directory in Windows, you would use the command dir instead of ls which is common in Unix-based systems. Users must be aware of these differences when writing cross-platform scripts.

Examples

Let's explore a few examples to demonstrate the usage of the os.system() method:

Example - 1: Listing Directory Contents

Consider my current folder has two text files and two folders. To list all files and directories in my current folder using os.system in python:

This code uses os.system() to execute the shell command ls, which lists all files and folders in the current directory.

Output:

Example - 2: Creating a New Directory

This code creates a new directory named new_folder using the mkdir shell command.

Output:

No output will be displayed if the command executes successfully. A new directory named new_folder will be created.

Example - 3: Retrieving Web Content with Wget

Here, os.system in python is used to call the wget command to fetch a web page

Output:

If successful, the content of the web page will be saved in the current directory.

Example - 4: Displaying System Information

This code retrieves system information using the uname -a shell command. The output includes details like the operating system version.

Output:

Example - 5: Downloading a File Using Curl

In this example, os.system in Python is employed to use the curl command for downloading a file from a specified URL. The -O flag instructs curl to save the file with the same name as the source.

Output:

If successful, the file will be downloaded and saved in the current directory.

Conclusion

  • The os.system() method in Python is used for the execution of shell commands directly from a Python script.
  • It takes command as a parameter which specifies the shell command to be executed.
  • The command does not return the command output but instead provides an exit code indicating the command's status.
  • Possible exceptions include OSError, FileNotFoundError, PermissionError, TypeError, and ValueError.
  • Python passes the command to the OS, which executes it in the shell environment and outputs it in the terminal.

See Also

The Python os.system() method is just one of many tools available in the os module for interacting with the operating system. Here are some related methods that you might find useful:

  • os.mkdir(): To create directories.
  • os.remove(): For deleting files.
  • os.rename(): To rename files.
  • subprocess module:
    For more complex interactions with the shell environment, consider exploring the subprocess module, which offers more flexibility and security compared to the os.system() method.
  • os.getcwd():
    To know the current working directory of your Python script.

You can learn more about these methods in OS module in Python