How to use functions commonly used in file/directory in OS module
GETCWD () returns the current working directory
ChDir (path) Change Working directory
Listdir (path= '. ') enumerates the file names in the specified directory ('. ') Represents the current directory, ' ... ' Indicates the previous level of the directory)
mkdir (path) creates a single-level directory, such that a thrown exception already exists in the directory
Makedirs (path) recursively creates a multi-level directory, such as the directory already throws an exception, note: ' e:\\a\\b ' and ' e:\\a\\c ' do not
Conflict
Remove file (path)
RmDir (path) deletes a single-level directory, which throws an exception if the directory is not empty
Removedirs (path) recursively deletes directories from subdirectories to parent directories and attempts to remove them, and throws an exception if the directory is not empty
Rename (old, new) rename the file old to new
System (Command) shell command to run the systems
Walk (top) traverses all subdirectories below the top path, returning a ternary group: (path, [include directory], [include file])
The following are some of the definitions that are commonly used in support path operations, supporting all platforms
Os.curdir refers to the current directory ('. ') )
Os.pardir refers to the upper level directory (' ... ') )
OS.SEP output operating system-specific path delimiter (win under ' \ \ ', Linux under '/')
Os.linesep the line terminator used by the current platform (win under ' \ r \ n ', Linux ' \ n ')
Os.name refers to the currently used operating system (including: ' POSIX ', ' NT ', ' Mac ', ' os2 ', ' CE ', ' java ')
How to use the function of path commonly used in Os.path module
Name of function |
How to use |
basename (PATH) |
Remove directory path, return file name separately |
DirName (PATH) |
Remove the file name and return the directory path separately |
Join (path1[, path2[, ...]) |
Combine the path1, path2 parts into a path name |
Split (PATH) |
Splits the file name with the path, returning the tuple (F_path, f_name). If the directory is fully used, it will also be the last A directory is separated as a file name and does not determine whether the files or directories exist |
Splitext (PATH) |
Detach file name and extension, return (f_name, f_extension) tuple |
GetSize (file) |
Returns the size of the specified file, in bytes |
Getatime (file) |
Returns the last access time of the specified file (floating-point number of seconds, gmtime () or localtime () of the available time module function conversions) |
Getctime (file) |
Returns the creation time of the specified file (the number of floating-point seconds, the gmtime () or localtime () function of the use time module Conversion |
Getmtime (file) |
Returns the latest modification time of the specified file (floating-point number of seconds, gmtime () or localtime () of the available time module function conversions) |
The following is a function that returns TRUE or False |
Exists (PATH) |
Determines whether the specified path (directory or file) exists |
Isabs (PATH) |
Determines whether the specified path is an absolute path |
Isdir (PATH) |
Determine if the specified path exists and is a directory |
Isfile (PATH) |
Determines whether the specified path exists and is a file |
Islink (PATH) |
Determines whether the specified path exists and is a symbolic link |
Ismount (PATH) |
Determine if the specified path exists and is a mount point |
Samefile (path1, PAHT2) |
Determine if the two paths of path1 and path2 point to the same file |
Python OS module, PATH module