Python OS module

Source: Internet
Author: User

OS module OS OS module is to operate the operating system, using the module must first import module: Import Osos module function getcwd () function: Get the current working directory format: OS.GETCWD () return value: Path string chdir () Function: Modify the current working directory format: Os.chdir () return value: Nonelistdir () function: Gets the list format of all files and folders in the specified folder: Os.listdir (directory path) return value: List of content names in the directory mkdir () Function: Create a directory/folder format: Os.mkdir (directory path) return value: Nonemakedirs () function: Recursively create folder format: Os.makedirs (PATH) rmdir () Function: Remove a directory (must be empty directory) format: Os.rmdir (directory path) return value: Noneremovedirs () function: Recursive delete folder format: Os.removedirs (directory path) return value: None Note: Delete d:/a/b/ c If there is no other file or folder in the ABC folder other than the folder shown in the path, Removedirs removes all folders a,b,c if any folder in ABC contains other files and folders, the folder will not be deleted. If it is the bottom of the C folder will be a non-empty error! Rename () function: Modify file and folder name format: os.rename (source file or folder, destination file or folder) return value: Nonestat () function: Get information about file format: Os.stat (file path) Return value: The tuple system () that contains the file Information System command format: Os.system () return value: Integer type use with caution! Accidentally to a rm-rf/want to cry no tears! Getenv () Function: Gets the system environment variable format: os.getenv (Gets the environment variable name) return value: String putenv () function: Set system environment variable format: os.putenv (' environment variable name ', value) Return value: No note: putenv does add success, but the exit () function cannot be detected with normal getenv: Launches the current execution command, directly closes the current action format: Exit () Return value: No value for the current OS module Curdiros.curdir function: Gets the current path is. Pardiros.pardir function: Gets the upper directory path is the. Pathos.path function: A sub-module in the OS, operation very many nameos.name function: The kernel name of the current system win->nt LinuX/unix->posixsepos.sep function: Gets the path of the current system split symbol window, \ linux/unix/ Extsepos.extsep function: Gets the split symbol between the file name and suffix in the current system, all systems are. linesepos.linesep function: Gets the line break symbol for the current system window, \ r \ n Linux/unix Nos.environ module Os.environ can directly get the information of all environment variables of the dictionary, if you want to change the environment variables, and can be queried, you need to operate on the Os.environ all methods of the module are dictionary methods, can be manipulated by the results of the dictionary Os.environ For Note: Whether using os.getenv,putenv or using Os.environ for environment variables is only for the current script, temporary settings, cannot be directly updated or operating system environment variable settings. The Os.path module Os.path is a submodule in the OS module and contains a number of path-related operations functions part abspath () function: Convert a relative path to absolute path format: Os.path.abspath (relative path) return value: Absolute path string basename () function: Gets the folder in the path or the file name (as long as the last part of the path) is formatted: os.path.basename (PATH) return value: The last part of the path (possibly the file name may also be the folder name) DirName ( ) function: Gets the path portion of the path (out of the last part) format: os.path.dirname (PATH) return value: In the path except for the last part of the content string join () function: Synthesize 2 paths in one path format: os.path.join (path 1, path 2) Return value: Merged path split () function: Cut a path into folder and file name part format: os.path.split (PATH) return value: Tuple Splitext () Function: Cut a file name into a name and suffix two part format: os.path.splitext (file name) return value: Tuple (name, suffix) getsize () function: Gets the size of a file format: os.path.getsize (PATH) return value: integer isfile () function: detects if a path is a file format: Os.path.isfile (PATH) return value: Boolean value Isdir () function: detects if a path is a folder format: Os.path.isdir (PATH) Return value: Boolean value GetctiMe () function: Gets the creation time of the file (get create times) format: os.path.getctime (file path) return value: Timestamp floating point number Getmtime () function: Gets the file's modified time (get modify TIME) Format: os.path.getmtime (file path) return value: Timestamp floating point number Getatime () feature: Get the file's access time (get active times) format: os.path.getatime (file path) return value: Timestamp floating point exists () function: detects whether the specified path exists in the format: os.path.exists (PATH) return value: Boolean isabs () function: detects if a path is an absolute path format: os.path.isabs (PATH) Return value: Boolean Islink () function: detects if a path is a link format: Os.path.islink (PATH) return value: Boolean samefile () function: detects whether 2 paths point to the same file format: os.path.samefile (Path 1, Path 2) return value: Boolean value

  

Python OS module

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.