


What is the os module in Python? Introduction to common methods of os module
This article brings you what is the os module in Python? This introduction to the common methods of the os module has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
os module: It can process files and directories. It is an interface for the Python system and the operating system to interact.
Common methods of the os module:
os.getcwd( ): Get the current working directory (that is, the directory path where the current Python script works)
os.chdir('dirname'): Change the working directory of the current script, equivalent to cd under the shell
os.system('bash command'): Run the shell command and display it directly (equivalent to starting a new shell and then executing that command. After the command execution is completed, the shell exits directly)
os. curdir: Returns the string name of the current directory
os.pardir: Returns the string name of the parent directory of the current directory
os.makedirs('dirname1/dirname2'): Can generate a multi-layer Recursive directory
os.removedirs('dirname1'): If the directory is empty, delete it and recurse to the upper-level directory. If it is also empty, delete it, and so on
os .mkdir('dirname'): Create a directory
os.rmdir('dirname'): Delete a directory. If the directory is not empty, it cannot be deleted and an error will be reported
os.listdir( 'dirname'): Display all files and subdirectories in the specified directory, including hidden files
os.remove(): Delete files
os.rename('oldname', 'newname'): Rename the file/directory. If the new file name/directory name already exists, an error will be reported
os.stat(' path/filename'): Get the file/directory information and can Get the size of the file
os.sep: Output the operating system-specific path separator, such as: win is '\\', Linux is '/'
os.liesep: Output the current The line terminator used by the platform, such as '\t\n' for win and '\n' for Linux
os.pathsep: Outputs the string used to split the file path
os. name: The output string indicates the current platform, such as win ->'nt'; Linux ->'posix'
os.environ: Get the environment variable of the operating system
os. path.abspath(path): Returns the normalized absolute path of path
os.path.split(path): Splits the path into a directory and file name tuple and returns
os.path. dirname(path): Returns the directory of path
os.path.basename(path): Returns the last file name of path (an absolute path only returns the last file name)
os.path .exists(path): Determine whether the path exists. If the path exists, return True; if it does not exist, return False
os.path.isabs(path): Determine whether it is an absolute path. If so, return True
os.path.isfile(path): Determine whether it is a file
os.path.isdir(path): Determine whether it is an existing directory
os .path.join(path1[,path2[,.....]]): Divide the path and file name into two elements in a list and put them together
os.path .getatime(path): Returns the last access time of the file or directory pointed to by path
os.path.getmtime(path): Returns the last modification time of the file or directory pointed by path
os.popen('dir'): Equivalent to opening a temporary file to store the opened directory (can be assigned to a variable, in the form of a string)
##The difference between system() and popen():
The above is the detailed content of What is the os module in Python? Introduction to common methods of os module. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Fastapi ...

Using python in Linux terminal...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
