OS of 6.python Common modules

Source: Internet
Author: User

The OS module is used to interact with 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-level recursive directories

Os.removedirs (' dirname1 ') if the directory is empty, then delete, and recursively to the previous level of the directory, if also empty, then delete.

Os.mkdir () creates a single directory.

Os.rmdir () deletes a single directory and cannot be deleted if the directory is not empty.

Os.listdir () lists all files or directories in the specified directory, returned as a list.

Os.remove () deletes a file.

Os.rename (' oldname ', ' newname ') can rename a file or directory.

Os.stat () Gets the detailed property information of the file or directory (for example: File size, uid,gid,inode number, atime,ctime,mtime, etc...) )

The os.sep is used to get the directory separator for the current operating system.

The os.linesep is used to get the default line break for the current operating system.

The os.pathsep is used to get the default path delimiter for the current operating system, which separates the symbols for each path.

Os.name used to get the platform used by the current system Windows returns ' NT '; Linux returns ' POSIX '.

Os.system () Run the system command directly, and the result after the command run, output directly to the screen, if the execution of the shell command under Linux, after the execution of the command, you can get the execution result of the shell command (0 is true, not 0 is false).

Os.popen () is used to run system commands, and the result of running the command can be saved to a Python variable.

The Os.environ is used to get environment variables in the current operating system.

Os.path.split () When the user enters a path to a file, the function in the module splits the path and file name into two parts, returning a tuple.

Os.path.abspath () in Abspath This function, the front end of any file name passed in (even if the file does not exist) is added to the absolute path of the directory where the Python program resides.

For example:

Print Os.path.abspath (' passwd ')

>>>/users/macbook/pycharmprojects/untitled1/pass


Os.path.dirname () returns the directory portion of the path. is actually the first element of Os.path.split (path).

Os.path.basename () returns the file portion of the path. is actually the second element of Os.path.split (path).

Os.path.exists () is used to detect if the path to a file really exists, if there is a return of true if there is no return false.

Os.path.isabs () is used to detect if a path is an absolute path, and if an absolute path returns TRUE, False if the absolute path is not returned.

Os.path.isfile () is used to detect if a file exists and returns True if there is no return false.

Os.path.ismount () is used to detect if a directory is a mount point, or False if True is returned.

Os.path.isdir () is used to detect if a directory exists and returns True if there is no return false.

Os.path.islink () is used to detect if a file is a linked file, which is true, otherwise returns false.

Os.path.join () is used for path stitching, stitching multiple paths together into a single path.

Os.path.getatime () is used to get the time that the file was last accessed. (returned as a timestamp.) )

Os.path.getctime () is used to get the time that the file property was last modified. (returned as a timestamp.) )

Os.path.getmtime () is used to obtain the last time the file content was modified. (returned as a timestamp.) )

Os.path.getsize () is used to get the size of the file. (The units returned are bytes.) )




This article is from the "Rebirth" blog, make sure to keep this source http://suhaozhi.blog.51cto.com/7272298/1910379

OS of 6.python Common modules

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.