Home > Article > Backend Development > Detailed explanation of os module in python
The os module provides new/deleted/viewed file attributes for directories or files, and also provides path operations for files and directories. For example: absolute path, parent directory...
os.sep
can replace operating system specific path separators. It is "\" under windows and "/" under Linux
os.linesep
String giving the line terminator used by the current platform. For example, Windows uses 'rn', Linux uses 'n' and Mac uses 'r'.
os.pathsep
Outputs a string used to split the file path. The system uses this character to split the search path (like PATH), such as ':' on POSIX, ';' on Windows
os.getcwd()
Get the current working directory, that is, the directory path where the current python script works
os.chdir("dirname")
Change the current script working directory; equivalent to cd under shell
os.curdir
Return to the current directory: ('.')
os.pardir
Get the parent directory string name of the current directory: ('..')
os.mkdir('dirname')
Generates a single-level directory; equivalent to mkdir dirname in the shell
os.makedirs('dirname1/dirname2') can generate multi-level recursive directories
os.remove(file)
Delete a file
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.rmdir('dirname')
Delete a single-level empty directory. If the directory is not empty, it cannot be deleted and an error will be reported; equivalent to rmdir dirname in the shell
os.listdir('dirname')
List all files and subdirectories in the specified directory, including hidden files, and print them in list mode
os.rename("oldname","newname")
Rename the file/directory, if newname exists, an error will occur when replacing it
os.replace(src,dest)
Rename the file/directory. If dest represents a file and exists, the original file will be overwritten and no error will be reported; if it is a directory, an error will be reported
os.chmod(path, mode, *, dir_fd=None, follow_symlinks=True)
Ex:os.chmod('C:\my_share_file\test.sh',755)
os.stat('path/filename')
Get file/directory information
os.utime(path, times)
Modify the time attribute times is a tuple, (atime, mtime), these two time numbers can be obtained through os.stat
os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
1.top represents the path of the directory tree that needs to be traversed
2. The default value of topdown is "True", which means that the files under the directory tree are first returned, and then the subdirectories of the directory tree are traversed. When the value of topdown is "False",
means first traversing the subdirectories of the directory tree, returning the files in the subdirectories, and finally returning the files in the root directory
3. The default value of onerror is "None", which means that errors generated during file traversal are ignored. If it is not empty, a custom function is provided to prompt the error message and then continue traversing or throw an exception to abort the traversal
This function returns a tuple, which has 3 elements. These 3 elements respectively represent the currently traversed directory, the currently traversed directory list, and the currently traversed directory file list
os.walk()
Example:
>>> import os >>> for root, dirs, files in os.walk("wd/chat", topdown=False): ... for name in files: ... print(os.path.join(root, name)) #打印文件绝对路径 ... for name in dirs: ... print(os.path.join(root, name)) #打印目录绝对路径
os.name
String indicating the platform you are using. For example, for Windows, it is 'nt', and for Linux/Unix users, it is 'posix'.
os.getenv()
Get an environment variable, if not return none
os.putenv(key, value)
Set an environment variable value
os.environ[]
Get the value of the environment variable. The two are equivalent: os.environ[‘HOME’] e09be6022d700e04aeaa85a5f42fdcb2os.getenv(‘HOME’)
os.system(command)
function is used to run shell commands.
os.popen("bash command")
Run the shell command to generate objects, which can be assigned to variables and then read with read