Python OS module introduction, pythonos Module
The OS module in the Python standard library contains common operating system functions. This module is especially important if you want your program to be unrelated to the platform. That is, it allows a program to run in Linux and Windows without any changes or any problems.
The following lists some useful parts in the OS module. Most of them are simple and clear.
OS. sep can replace the path delimiter specified by the operating system.
The OS. name string indicates the platform you are using. For example, for Windows, it is 'nt ', and for Linux/Unix users, it is 'posix '.
The OS. getcwd () function obtains the current working directory, that is, the directory path of the current Python script.
The OS. getenv () and OS. putenv () functions are used to read and set environment variables respectively.
OS. listdir () returns all files and directory names in the specified directory.
The OS. remove () function is used to delete an object.
The OS. system () function is used to run shell commands.
The OS. linesep string specifies the row Terminator used by the current platform. For example, in Windows, '\ r \ n' is used, in Linux,' \ n' is used, and in Mac, '\ R' is used '.
OS. listdir (dirname) List directories and files under dirname
OS. getcwd () to get the current working directory
OS. curdir: returns the current directory ('.')
OS. chdir (dirname) changes the working directory to dirname
OS. path. abspath (path) returns the absolute path.
OS. path. basename (path) returns the file name.
OS. path. commonprefix (list) returns the longest paths in the list (multiple paths.
OS. path. dirname (path) returns the file path
If the OS. path. exists (path) path exists, True is returned. If the path is damaged, False is returned.
If the OS. path. lexists path exists, True is returned. If the path is damaged, True is returned.
OS. path. expanduser (path "~ "And "~ Convert user to user directory
OS. path. expandvars (path) replaces the "$ name" and "$ {name}" contained in the path based on the environmental variable values }"
OS. path. getatime (path) returns the last time this path was entered.
OS. path. getmtime (path) returns the last modification time under this path.
OS. path. getctime (path) returns the size of the path.
OS. path. getsize (path) returns the file size. If the file does not exist, an error is returned.
OS. path. isabs (path) determines whether it is an absolute path
OS. path. isfile (path) determines whether the path is a file
OS. path. isdir (path) determines whether the path is a directory
OS. path. islink (path) determines whether the path is a link
OS. path. ismount (path) determines whether the path is a mount point ()
OS. path. join (path1 [, path2 [,...]) combines the Directory and file name into a path
OS. path. normcase (path) converts the case and slash of path
OS. path. normpath (path) standard path string format
OS. path. realpath (path) returns the true path of the path.
OS. path. relpath (path [, start]) calculates the relative path from start.
OS. path. samefile (path1, path2) determines whether the directory or file is the same
OS. path. sameopenfile (fp1, fp2) determines whether fp1 and fp2 point to the same file
OS. path. samestat (stat1, stat2) judge whether stat tuple stat1 and stat2 point to the same file
OS. path. split (path) splits the path into dirname and basename, and returns a tuple.
OS. path. splitdrive (path) is generally used in windows and returns a tuple consisting of the drive name and path.
OS. path. splitext (path) Split path, returns the path name and file extension tuples
OS. path. splitunc (path) splits the path into a load point and a file
OS. path. walk (path, visit, arg) traverses the path and calls the visit function in each directory. The visit function must have three parameters (arg, dirname, names ), dirname indicates the Directory Name of the current directory, names indicates all file names in the current directory, and args indicates the third parameter of the walk.
OS. path. supports_unicode_filenames: Specifies whether unicode path names are supported.