Python OS module, PATH module

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.