Python OS module

Source: Internet
Author: User

So the Python author daoteng the OS module 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.


How to use functions commonly used in file/directory in OS module



Name of function How to use
GETCWD () Return to current working directory
ChDir (PATH) Change the 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) Create a single-level directory, such that a thrown exception already exists in the directory
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
Remove (PATH) deleting files
RmDir (PATH) Delete a single-level directory, which throws an exception if the directory is not empty
Removedirs (PATH) Recursively delete directories, from subdirectories to parent directories, and try to delete them, and throw exceptions when the directory is not empty
Rename (old, new) Rename the file old to new
System (Command) Shell command to run the system
Walk (top) Iterate through all subdirectories below the top path, returning a ternary group: (path, [include directory], [include file]) "Concrete implementation of the program see: 30th Lecture after the assignment ^_^"
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 also separates the last directory as a file name and does not determine whether the file or directory exists
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, which can be converted using the gmtime () or localtime () function of the time module)
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)
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
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

Transferred from: http://bbs.fishc.com/forum.php?mod=viewthread&tid=45512&extra=page%3D1%26filter%3Dtypeid%26typeid% 3d403

Python OS 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.