python os module

OS module
Abbreviation for OS

The os module is to operate on the operating system. To use this module, you must first import the module:

import them
functions in the os module
getcwd()
Function: Get the current working directory
Format: os.getcwd()
Return value: path string
chdir()
Function: Modify the current working directory
Format: os.chdir()
Return value: None
listdir ()
Function: Get a list of all files and folders in the specified folder
Format: os.listdir (directory path)
Return value: List of content names in the directory
mkdir()
Function: Create a directory/folder
Format: os.mkdir (directory path)
Return value: None
makedirs()
Function: recursively create folders
Format: os.makedirs(path)
rmdir()
Function: remove a directory (must be an empty directory)
Format: os.rmdir (directory path)
Return value: None
removedirs()
Function: delete folders recursively
Format: os.removedirs (directory path)
Return value: None
Note: For example: delete D:/a/b/c

If there are no other files or folders in the abc folder except the folder shown in the path, removedirs will remove all folders a, b, c
If any abc folder contains other files and folders, the folder will not be deleted, and if it is the bottom c folder, a non-empty error will pop up!
rename()
Function: Modify the names of files and folders
Format: os.rename(source file or folder, destination file or folder)
Return value: None
stat()
Function: Get the relevant information of the file
Format: os.stat (file path)
Return value: tuple containing file information
system()
execute system commands
Format: os.system()
Return value: Integer

Use with caution! Accidentally rm -rf / I want to cry without tears!
getenv()
Function: Get system environment variables
Format: os.getenv (acquired environment variable name)
Return value: string
putenv()
Function: Set system environment variables
Format: os.putenv('environment variable name', value)
Return value: none
Note: putenv can indeed be added successfully, but cannot be detected using normal getenv
exit()
Function: Launch the current execution command and directly close the current operation
Format: exit()
Return value: none

The value of the current os module
curdir
os.curdir
Function: Get the current path is .
leave
the.pardir
Function: Get the upper directory path is ..
path
os.path
Function: a submodule in os, with a lot of operations
name
os.name
Function: the kernel name of the current system win->nt linux/unix->posix
sep
os.sep
Function: Get the path segmentation symbol of the current system window -> \ linux/unix -> /
extsep
os.extsep
Function: Get the separation symbol between the file name and suffix in the current system, all systems are .
linesep
os.linesep
Function: Get the newline symbol of the current system window -> \r\n linux/unix -> \n
os.environ module
os.environ
You can directly obtain a dictionary of information about all environment variables. If you want to change the environment variables and you can query them, you need to operate on os.environ
All methods of this module are dictionary methods and can be manipulated through the result of the dictionary's os.environ.
Note: Whether you use os.getenv, putenv or os.environ to operate environment variables, it is only for the current script, temporary settings, and cannot be directly updated or the environment variable settings of the operating system.
os.path module
os.path is a submodule in the os module, which contains many path-related operations
function part
abspath()
Function: Convert a relative path to an absolute path
Format: os.path.abspath (relative path)
Return value: absolute path string
basename()
Function: Get the folder or file name in the path (as long as the last part of the path)
Format: os.path.basename(path)
Return value: the last part of the path (may be a file name or a folder name)
dirname()
Function: Get the path part in the path (go out the last part)
Format: os.path.dirname (path)
Return value: The content string of the path except the last part
join()
Function: Combine 2 paths into one path
Format: os.path.join(path1,path2)
Return value: merged path
split()
Function: Cut a path into folder and file name parts
Format: os.path.split(path)
Return value: tuple
splitext()
Function: Cut a file name into two parts: name and suffix
Format: os.path.splitext (file name)
Return value: tuple (name, suffix)
getsize()
Function: Get the size of a file
Format: os.path.getsize(path)
Return value: integer
isfile()
Function: Check if a path is a file
Format: os.path.isfile(path)
Return value: boolean
isdir()
Function: Check if a path is a folder
Format: os.path.isdir(path)
Return value: boolean
getctime()
Function: Get the creation time of the file (get create time)
Format: os.path.getctime (file path)
Return value: timestamp float
getmtime()
Function: Get the modification time of the file (get modify time)
Format: os.path.getmtime (file path)
Return value: timestamp float
getatime()
Function: Get the access time of the file (get active time)
Format: os.path.getatime (file path)
Return value: timestamp float
exists()
Function: Check whether the specified path exists
Format: os.path.exists(path)
Return value: boolean
isabs()
Function: Check whether a path is an absolute path
Format: os.path.isabs(path)
Return value: boolean
islink()
Function: Check if a path is a link
Format: os.path.islink(path)
return value: boolean
samefile()
Function: detect whether two paths point to the same file
Format: os.path.samefile(path1,path2)
Return value: boolean

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324715013&siteId=291194637