Python OS module features and methods summary

Source: Internet
Author: User

1 OS.SEP can replace operating system-specific path delimiters2The OS.LINESEP string gives the line terminator used by the current platform. For example, Windows uses'\ r \ n', Linux uses'\ n'and Mac uses'\ r'. 3The Os.name string indicates the platform you are using. For Windows, for example, it is'NT', and for Linux/unix users, it is'POSIX'4 the OS.GETCWD () function gets the current working directory,5 The os.getenv () and os.putenv () functions are used to read and set environment variables, respectively. 6 Os.listdir (dirname): List directories and files under DirName7 The Os.remove () function is used to delete a file. 8Os.curdir: Return but previous directory ('.')9 Os.chdir (dirname): Changing the working directory to dirnameTen getatime (PATH): The last access time of a file or folder, from the new era to the number of seconds it was accessed One getmtime (PATH): Last modified time for file or folder A getctime (PATH): The time the file or folder was created -  - Os.path module: the The os.path.isfile () and Os.path.isdir () functions respectively verify whether the given path is a file or a directory, and returns a bool value - the os.path.exists () function is used to verify that the given path really exists to return bool - os.path.getsize (name): Gets the file size, if name is directory returns 0L returns a long unit of bytes - Os.path.abspath (name): Get absolute path +Os.path.normpath (PATH): Canonical path string form, result in general/change//, - os.path.split (name): divides name into pathname and file name, resulting in (pathname, filename. file extension) (in fact, if you use the directory completely, it will also separate the last directory as a file name, and it will not determine whether the file or directory exists)  +Os.path.splitext (filename): detaches the file name with the extension result (filename, extension) If the parameter is a path then return (path,"') AOs.path.join (path,name): Connection directory with file name or directory result is path/name atOs.path.basename (PATH): Returns the file name actually the last one to put path"/"Split and return to the latter. Regardless of whether the parameter is a path or the file is the same as the Os.path.split (name), the latter returns two worth of tuples -  -Os.path.dirname (PATH): Returns the file path is actually the last one to put path"/"Split, returning the former. Whether the parameter is a path or a file - The Os.system () function is used to run shell commands -  - The above is only common and lists all of the following: in  - The OS module wraps the common interfaces of different operating systems, allowing the user to use the same function interface to return the results of the same structure under different operating systems.  toOs.name: Returns the current operating system name ('POSIX','NT','Os2','mac','CE' or 'Riscos') + The OS defines a set of file and path representation parameters in different operating systems, such as - os.sep (Folder delimiter, Windows is \) the os.extsep (the extension delimiter, which is in Windows.) * os.pathsep (Directory delimiter, in Windows;) $ os.linesep (newline delimiter, \ r \ n in Windows)Panax Notoginseng There are a number of related functions for file and path operations in the OS, such as: - Listdir (PATH): Lists all files under the directory the MakeDir (PATH): Create folder, note: Create an existing folder with exception + makedirs (path): Recursive creation of folders, note: Creating an existing folder will be an exception A Remove (filename): Delete a file the rmdir (path): Delete a folder, note: Deleting a non-empty folder will be an exception +Removedirs (PATH): Recursively delete folders until a folder with one level is not empty, note: The folder path cannot be'\ ' End - rename (SRC,DST): Renaming a file or folder (you can change the path, but you cannot overwrite the target file) $ renames (SRC,DST): Recursive renaming of files or filenames $ Walk (PATH): Lists all files and folders under Path - process-related operations in the OS, such as: - execl (PATH): Runs a program to replace the current process, which can be blocked the _exit (N): Exiting the program - startfile (filename): Runs with the program associated with the file, and immediately returns when the associated program is openedWuyi System (CMD): Runs a program or command that returns immediately and returns a CMD exit code after cmd execution completes the Os.path: Calling different modules in different operating systems is an import module that provides a number of useful operations in this module: - Abspath (PATH): Returns the absolute path and remains if path is already an absolute path.  Wu basename (PATH): Returns the file name in path.  - Commonprefix (list): Returns the uniform prefix in the list used to get the same left-hand content of a set of strings AboutDirName (PATH): Returns the folder portion of path, with the result not containing'\ ' $ exists (PATH): Whether a file or folder exists - getatime (PATH): The last access time of a file or folder, from the new era to the number of seconds it was accessed - getmtime (PATH): Last modified time for file or folder - getctime (PATH): The time the file or folder was created A getsize (PATH): The size of the file or folder, if the folder returns 0 + isabs (PATH): Returns whether it is an absolute path the isfile (PATH): Returns whether it is a file path - isdir (PATH): Returns whether it is a folder path $ Islink (PATH): Returns whether it is a shortcut the Join (Path1,path2,...): Combines path and, if there is an absolute path, the previous path is deleted the normcase (PATH): Delimiter in the transform path the Normpath (PATH): The path to the system is recognized by the conversion path the Realpath (path): Convert path to absolute path - Split (path): Breaks the path into (folder, file name) in splitext (path): Breaks the path into (the remainder,. extension), if there is no extension in the file name, the extension part is an empty string theThrows a OSError exception when operating with an object that is not supported by the system.
View Code

Python OS module features and methods summary

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.