Use of Python OS Library

Source: Internet
Author: User
The pythonos library has many operating system-related functions. In fact, this is not the case. There are many OS libraries related to files, paths, and execution of system commands. The following are common methods for operating system modules. 1. OS. sep can replace the path delimiter specified by the operating system. 2. OS. name string indicates the platform you are using. For example, for Windows, it is nt, and for Linux and Unix users, it... the python OS library has many operating system-related functions.

In fact, this is not the case. There are many OS libraries related to files, paths, and execution of system commands.

The following are common methods for operating system modules.

1. OS. sep can replace the path delimiter specified by the operating system
2. the OS. name string indicates the platform you are using. For example, for Windows, it is 'nt ', and for Linux/Unix users, it is 'posix'
3. the OS. getcwd () function obtains the current working directory, that is, the directory path of the current Python script.

4. OS. getenv () is used to read environment variables.

5. OS. putenv () is used to set environment variables.
6. OS. listdir () returns all files and directory names in the specified directory.
The 7os. remove () function is used to delete an object.
8. the OS. system () function is used to run shell commands.

9. the OS. linesep string indicates the row Terminator used by the current platform. For example, in Windows, '/r/N' is used, in Linux,'/N' is used, and in Mac, '/r' is used '.

10. the OS. path. split () function returns the directory name and file name of a path.

11. OS. path. isfile () check that the given path is a file

12os. path. isdir () indicates that the given path is a directory.

13. the OS. path. existe () function is used to check whether the given path actually exists.

14. OS. listdir (dirname) lists directories and files under dirname
15. OS. getcwd () get the current working directory
16. OS. curdir returned but the former Directory ('.')
17. OS. chdir (dirname) changes the working directory to dirname

OS. path is a function related to the specified file, directory, and path string.

OS. path. isdir (name) determines whether the name is a directory. if the name is not a directory, false is returned.
OS. path. isfile (name) determines whether the name is a file. if the name does not exist, false is returned.
OS. path. exists (name) to determine whether a file or directory name exists
OS. path. getsize (name) to get the file size. if the name is a directory, return 0L
OS. path. abspath (name) to obtain the absolute path
OS. path. normpath (path) standard path string format
OS. path. split (name) splits the file name and Directory (in fact, if you fully use the directory, it also splits the last directory as the file name

And does not determine whether a file or directory exists)
OS. path. splitext () separation of file names and extensions
OS. path. join (path, name) connection directory and file name or directory
OS. path. basename (path) returns the file name.
OS. path. dirname (path) returns the file path

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.