Home  >  Article  >  Backend Development  >  How to use the os module to make system calls in Python 2.x

How to use the os module to make system calls in Python 2.x

WBOY
WBOYOriginal
2023-07-30 17:53:201020browse

How to use the os module to make system calls in Python 2.x

Introduction:
The os module is an important module in Python used to interact with the operating system. It contains many functions that can execute system calls, allowing us to conveniently process files and directories, execute external commands, and obtain and set the system environment in Python. This article will introduce how to use the os module in Python 2.x to make system calls and give code examples.

1. File and directory operations

  1. Get the current working directory
    The os.getcwd() function is used to get the path of the current working directory. For example, we can use the following code to get the current working directory and print it out:
import os

# 获取当前工作目录
current_dir = os.getcwd()
print("当前工作目录:{}".format(current_dir))
  1. Switch directory
    os.chdir(path) function can be used to switch the working directory to the specified path . For example, we can use the following code to switch the working directory to "/home/user/Documents":
import os

# 切换工作目录至 "/home/user/Documents"
os.chdir("/home/user/Documents")
  1. Create Directory
    os.mkdir(path) function can be used to specify Path creates a new directory. For example, we can use the following code to create a new directory named "test" in the current working directory:
import os

# 在当前工作目录下创建名为 "test" 的目录
os.mkdir("test")
  1. Delete directory
    os.rmdir(path) function can be used To delete the directory at the specified path. Note that this function can only delete empty directories. For example, we can use the following code to delete the directory named "test" in the current working directory:
import os

# 删除当前工作目录下的名为 "test" 的目录
os.rmdir("test")
  1. List all files and subdirectories in the directory
    os.listdir(path ) function can be used to list all files and subdirectories under a specified path. For example, we can use the following code to list all files and subdirectories in the current working directory:
import os

# 列出当前工作目录下的所有文件和子目录
files = os.listdir(".")
for file in files:
    print(file)

2. Execute external commands

The os module also provides some functions to execute external Order.

  1. Execute command
    os.system(command) function can be used to execute specified external commands. For example, we can use the following code to execute the "ls" command on the command line:
import os

# 执行 "ls" 命令
os.system("ls")
  1. Get command output
    os.popen(command) function can be used to execute the specified external command and can get the output of the command. For example, we can use the following code to execute the "ls" command and obtain the output:
import os

# 执行 "ls" 命令,并获取输出
output = os.popen("ls").read()
print(output)

3. Obtain and set the system environment

The os module also provides some functions for obtaining and Set system environment variables.

  1. Get environment variables
    The os.environ.get(variable) function can be used to obtain the value of the specified environment variable. For example, we can use the following code to get the value of the "PATH" environment variable:
import os

# 获取 "PATH" 环境变量的值
path = os.environ.get("PATH")
print(path)
  1. Set environment variables
    os.environ[variable] = value can be used to set the specified environment variable value. For example, we can use the following code to set the value of the "CUSTOM_VAR" environment variable to "custom_value":
import os

# 设置 "CUSTOM_VAR" 环境变量的值为 "custom_value"
os.environ["CUSTOM_VAR"] = "custom_value"

Conclusion:

The os module is a module in Python used to make system calls Important modules. Through the os module, we can easily perform file and directory operations, execute external commands, and obtain and set the system environment. In this article, we introduce some common functions of the os module and give relevant code examples. By learning and using the os module, we can better handle and control the operation of the operating system.

The above is the detailed content of How to use the os module to make system calls in Python 2.x. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn