A detailed introduction to the operating system of common python modules

Source: Internet
Author: User
This article describes the operating system of common python modules in detail, which has some reference value. if you are interested, refer to the operating system module for interaction between python and the operating system.

Common functions are as follows:

OS. getcwd () gets the current working path of the python script.

OS. chdir () modifies the working path of the current script.

OS. makedirs ('dirname1/dirname2 ') can generate multi-layer recursive directories.

OS. removedirs ('dirname1') if the directory is empty, delete it and recursively go to the upper-level Directory. if the directory is empty, delete it.

OS. mkdir () to create a single directory.

OS. rmdir () deletes a single directory. if the directory is not empty, it cannot be deleted.

OS. listdir () is used to list all files or directories under a specified directory.

OS. remove () delete an object.

OS. rename ('oldname', 'newname') can be used to rename a file or directory.

OS. stat () obtains detailed attribute information of a file or directory (for example, file size, uid, gid, inode, atime, ctime, mtime, etc ....)

OS. sep is used to obtain the directory delimiter of the current operating system.

OS. linesep is used to obtain the default line break of the current operating system.

OS. pathsep is used to obtain the default path separator of the current operating system, which is used to separate the symbols of each path.

OS. name is used to obtain the platform used by the current system. Windows returns 'nt '; Linux returns 'posix '.

OS. system () directly runs the system command and outputs the result after the command is run to the screen. if the command is run in linux, after the command is executed, you can obtain the execution result of this shell command (0 is true, not 0 is false ).

OS. popen () is used to run system commands and save the running results to python variables.

OS. environ is used to obtain environment variables in the current operating system.

OS. path. split () when the user inputs the path of a file, the function in the module splits the path and file name into two parts and returns a tuple.

OS. path. abspath () in the abspath function, the front end of any input file name (even if the file does not exist) will be added with the absolute path of the directory where the python program is located.

For example:

Print OS. path. abspath ('passwd ')

>>>/Users/macbook/PycharmProjects/untitled1/pass

OS. path. dirname () returns the directory part of the path. It is actually the first element of OS. path. split (path.

OS. path. basename () indicates the part of the file in the returned path. It is actually the second element of OS. path. split (path.

OS. path. exists () is used to check whether the path of a file exists. If yes, True is returned. If no path exists, False is returned.

OS. path. isabs () is used to check whether a path is an absolute path. if it is an absolute path, True is returned. if it is not an absolute path, False is returned.

OS. path. isfile () is used to check whether a file exists. If yes, True is returned. If no file exists, False is returned.

OS. path. ismount () is used to check whether a directory is a Mount point. If yes, True is returned; otherwise, False is returned.

OS. path. isdir () is used to check whether a directory exists. If yes, True is returned. If no directory exists, False is returned.

OS. path. islink () is used to detect whether a file is a linked file. True is returned; otherwise, False is returned.

OS. path. join () is used to concatenate multiple paths into one path.

OS. path. getatime () is used to obtain the time when the file was last accessed. (Return with a timestamp .)

OS. path. getctime () is used to obtain the last modified time of the file attribute. (Return with a timestamp .)

OS. path. getmtime () is used to obtain the last modified time of the file content. (Return with a timestamp .)

OS. path. getsize () is used to obtain the file size. (The unit returned is byte .)

The above is a detailed introduction to the OS of common python modules. For more information, see other related articles in the first PHP community!

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.