How to use the os module to make system calls in Python 3.x
Introduction:
In Python 3.x, the os module provides many functions that can be used to interact with the operating system. These functions include file and directory operations, process management, access to environment variables, etc. This article will focus on how to use the os module to make system calls and provide some code examples.
1. Import the module
Before you start using the os module, you first need to import the module. You can use the following code to import the os module:
import os
2. Commonly used system call functions
The os module provides many system call functions. Here are some commonly used functions and their brief descriptions:
os.system(command)
This function can be used to execute operating system commands. It accepts a string parameter representing the command to execute. For example, to run the "dir" command on a Windows system, you can use the following code:
os.system("dir")
os.getenv(key)
This function is used to obtain the value of the specified environment variable . It accepts a string parameter representing the name of the environment variable. If the specified environment variable exists, its value is returned; otherwise, None is returned. For example, to get the value of the "PATH" environment variable, you can use the following code:
path = os.getenv("PATH") print(path)
os.chdir(path)
This function is used to change the current working directory. It accepts a string parameter representing the path to the directory to switch to. For example, to change the current working directory to "/home/user", you can use the following code:
os.chdir("/home/user")
os.getcwd()
This function is used to obtain the current working directory path. For example, to print the path of the current working directory, you can use the following code:
cwd = os.getcwd() print(cwd)
os.mkdir(path)
This function is used to create a new directory. It accepts a string parameter representing the path to the directory to be created. For example, to create a directory named "new_dir", you can use the following code:
os.mkdir("new_dir")
os.rmdir(path)
This function is used to delete a directory. It accepts a string parameter representing the path to the directory to be deleted. It should be noted that deletion can only be successful if the directory is empty. For example, to delete a directory named "old_dir", you can use the following code:
os.rmdir("old_dir")
os.listdir(path)
This function is used to get all files in the specified directory and a list of folders. It accepts a string parameter representing the path to the specified directory. The returned list contains the names of all items in the directory. For example, to get a list of all files and folders in the current directory, you can use the following code:
items = os.listdir(".") print(items)
os.rename(src, dst)
This function is used to rename Name a file or directory. It accepts two string parameters, representing the original file or directory name and the new file or directory name. For example, to rename a file named "old_file.txt" to "new_file.txt", you can use the following code:
os.rename("old_file.txt", "new_file.txt")
3. Code examples
The following are some uses Code example of system call made by os module:
Execute the command and output the result:
import os # 执行"dir"命令并输出结果 os.system("dir")
Get the value of the environment variable:
import os # 获取"PATH"环境变量的值 path = os.getenv("PATH") print(path)
Change the current working directory:
import os # 将当前工作目录更改为"/home/user" os.chdir("/home/user")
Get the path of the current working directory:
import os # 打印当前工作目录的路径 cwd = os.getcwd() print(cwd)
Create a new one Directory:
import os # 创建名为"new_dir"的目录 os.mkdir("new_dir")
Delete a directory:
import os # 删除名为"old_dir"的目录 os.rmdir("old_dir")
Get a list of all files and folders in a directory:
import os # 获取当前目录中的所有文件和文件夹的列表 items = os.listdir(".") print(items)
Rename a file or directory:
import os # 将名为"old_file.txt"的文件重命名为"new_file.txt" os.rename("old_file.txt", "new_file.txt")
Summary:
This article introduces the basic knowledge of using the os module to make system calls in Python 3.x, and Some commonly used code examples are provided. By learning and using these functions of the os module, you can interact with the operating system more conveniently and complete various system operations. Hope this article helps you!
The above is the detailed content of How to use the os module to make system calls in Python 3.x. For more information, please follow other related articles on the PHP Chinese website!