Detailed Os,os.path module about the common methods of file directory

Source: Internet
Author: User
Python is a cross-platform language, which means that the same source code can be implemented in different operating systems without the need for modification.

With the OS module, we don't have to worry about what the operating system is using, and the OS module will help you choose the right module and call it.

Outfit Name of function How to use
1 GETCWD () Return to current working directory
2 ChDir (PATH) Change the working directory
3 Listdir (path= '. ') Enumerates the file names in the specified directory ('. ') Represents the current directory, ' ... ' Indicates the previous level of the directory)
4 mkdir (PATH) Create a single-level directory, such that a thrown exception already exists in the directory
5 Makedirs (PATH) Recursive creation of multi-level catalogs, such as the directory already exists thrown exception, note: ' e:\\a\\b ' and ' e:\\a\\c ' do not conflict
6 Remove (PATH) deleting files
7 RmDir (PATH) Delete a single-level directory, which throws an exception if the directory is not empty
8 Removedirs (PATH) Recursively delete directories, from subdirectories to parent directories, and try to delete them, and throw exceptions when the directory is not empty
9 Rename (old, new) Rename the file old to new
10 System (Command) Commands to run the system
11 Walk (top) Iterate through 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

Outfit Name of function How to use
1 Os.curdir Refers to the current directory ('. ') )
2 Os.pardir refers to the upper level directory ('.. ') )
3 Os.sep Output operating system-specific path delimiter (win under ' \ \ ', Linux under '/')
4 Os.linesep The line terminator used by the current platform (win under ' \ r \ n ', Linux ' \ n ')
5 Os.name Refers to the operating system currently in use

How to use the function of path commonly used in Os.path module

Outfit Name of function How to use
1 basename (PATH) Remove directory path, return file name separately
2 DirName (PATH) Remove the file name and return the directory path separately
3 Join (path1[, path2[, ...]) Combine the path1, path2 parts into a path name
4 Split (PATH) Splits the file name with the path, returning the tuple (F_path, f_name). If the directory is fully used, it also separates the last directory as a file name and does not determine whether the file or directory exists
5 Splitext (PATH) Detach file name and extension, return (f_name, f_extension) tuple
6 GetSize (file) Returns the size of the specified file, in bytes
7 Getatime (file) Returns the last access time of the specified file (floating-point number of seconds, which can be converted using the gmtime () or localtime () function of the time module)
8 Getctime (file) Returns the creation time of the specified file (floating-point number of seconds, which can be converted using the gmtime () or localtime () function of a time module)
9 Getmtime (file) Returns the latest modification time of the specified file (floating-point number of seconds, gmtime () or localtime () function conversion of the time module)

The following is a function that returns TRUE or False

designation Function name how to use
1 exists (path) determines whether a specified path (directory or file) exists
2 isabs (path) determines whether the specified path is an absolute path
3 isdir (path) determines whether the specified path exists and is a directory
4 isfile (path) Determines whether the specified path exists and is a file
5 islink (path) determines whether the specified path exists and is a symbolic link
6 ismount (path) determines whether the specified path exists and is a mount point
7 SAMEF Ile (path1, paht2) determines whether path1 and path2 two paths point to the same file
Related Article

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.