Python series------function type--os function library

Source: Internet
Author: User

We learn the basics of OS libraries from the level of the operating system:

1. Type of operating system

2. Execute operating system commands

3, the location of the path

4. Create a Directory

5. Delete Files

6. Delete Directory

7. Change the path

8. Get content under the catalogue

1 , Os.name functions

function: Gets the operating system currently in use (not enough information to get detailed)

where ' NT ' is windows, ' POSIX ' is Linux or Unix

>>> Import OS

>>> System_type=os.name

>>> if system_type== ' NT ':

Print (' SystemType is windows! ')

Elif system_type== ' POSIX ':

Print (' SystemType is Linux or unix! ')

Else

Print (' SystemType is other! ')

After running, the output is as follows:

System type is windows!

2 , Os.system () function

function: Run shell command

>>> Import OS

>>> os.system (' dir ')

3 , OS.GETCWD () function

function: Get the current directory, Python's work

>>> Import OS

>>> PWD=OS.GETCWD ()

>>> Print (PWD)

After running, the output is as follows:

D:\software\12.python

4 , Os.mkdir () function

function: Create a new directory

eg : Create a test directory under the system D drive

>>> Import OS

>>> os.mkdir (' d:\\test ')

5 , Os.removedirs () function

Function: Delete the specified directory

eg : Delete Test directory under D drive

>>> Import OS

>>> os.removedirs (' d:\\test ')

6 , Os.remove () function

Function: Delete the specified file

eg : delete file.txt file

>>>os.chdir (' D:\\test ')

>>>OS.GETCWD ()

' D:\\test '

>>>os.remove (' file.txt ')

Note: This function can only delete files in the current directory

7 , Os.chdir () function

function: Change the current path to the specified path

eg : I now go from the current path to the path specified by filepath

>>> Import OS

>>> PWD=OS.GETCWD ()

>>> Print (PWD)

D:\test

>>> filepath= ' D:\\download '

>>> Os.chdir (filepath)

>>> PWD=OS.GETCWD ()

>>> Print (PWD)

D:\download

8 , Os.listdir () function

function: Returns all directories and files under the specified directory

eg : Lists all files and directories in the current directory

>>>import OS

>>>PWD=OS.GETCWD ()

>>>print (PWD)

D:\download







This article from "Technology bo" blog, declined reprint!

Python series------function type--os function library

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.