Python Module Learning: OS module

Source: Internet
Author: User

I. Overview of OS Modules

The Python OS module contains common operating system features. This module is especially important if you want your program to be platform-agnostic. (in one's language)

Second, common methods

1, Os.name

The output string indicates the platform being used. If the window is ' NT ', it is ' POSIX ' for Linux/unix users.

2, OS.GETCWD ()

The function gets the current working directory, which is the directory path of the current Python script work.

3, Os.listdir ()

Returns all file and directory names under the specified directory.

4, Os.remove ()

Deletes a file.

5, Os.system ()

Run the shell command.

>>> os.system (' dir ') 0>>> os.system (' cmd ') #启动dos

6. OS.SEP can replace operating system-specific path separators.

7. Os.linesep string gives the line terminator used by the current platform

>>> os.linesep ' \ r \ n '            #Windows使用 ' \ r \ n ', Linux uses ' \ n ' and Mac uses ' \ R '. >>> os.sep ' \ \ '              

8, Os.path.split ()

function returns the directory name and file name of a path

>>> os.path.split (' C:\\python25\\abc.txt ') (' C:\\python25 ', ' abc.txt ')

The 9, Os.path.isfile (), and Os.path.isdir () functions respectively verify that the given path is a file or a directory.

>>> Os.path.isdir (OS.GETCWD ()) true>>> os.path.isfile (' a.txt ') False

10, Os.path. The exists() function is used to verify that the given path really exists

11. Os.path.abspath (name): Get absolute path

12, Os.path.normpath (PATH): Canonical path string form

13, Os.path.getsize (name): Get file size, if name is directory return 0L

14, Os.path.splitext (): Detach file name and extension

>>> os.path.splitext (' a.txt ') (' a ', '. txt ')

15, Os.path.join (path,name): Connection directory and file name or directory

16. Os.path.basename (PATH): Return file name

17. Os.path.dirname (PATH): Return file path

>>> os.path.dirname (' c:\\python\\a.txt ') ' C:\\python '

Python Module Learning: 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.