Python3.2 Official document Translation--Overview of the standard library (i)

Source: Internet
Author: User
Tags glob

7.1Operating System Interface

The OS module provides many of the main functions that interact with the operating system.

>>> Import OS

>>> os.getcwd () # Return the current working directory

'c:\\python31'

>>> os.chdir ('/server/accesslogs') # Change current working directory

>>> Os.system ('mkdir today') # Run the command mkdir in the system shell

0

Be sure to replace the FROM OS import * with the import OS method . this will make Os.open () method overrides the built -in Open () function.

Because they operate in very different ways.

The built-in methods of the dir () and help () methods are useful for interacting with large modules such as the OS .

>>> Import OS

>>> dir (OS)

<returns a list of all module functions>

>>> Help (OS)

<returns an extensive manual page created from the module's docstrings>

For daily file and folder management tasks, theshutill module provides a higher-level and user-friendly interface.

>>> Import Shutil

>>> shutil.copyfile ('data.db', 'archive.db' )

>>> shutil.move ('/build/executables', ' InstallDir')

7.2File wildcard characters

The Glob module provides a function used to search for a list of production files from a folder wildcard.

>>> Import Glob

>>> Glob.glob ('*.py')

['primes.py', 'random.py', ' quote.py ' ]

7.3command line number of parameters

Common tool scripts often need to provide command-line parameters. These parameters are saved as a list in the argv property of the sys module . Like what. The output is then obtained by executing python demo.py One, three, on the command line .

>>> Import Sys

>>> Print (SYS.ARGV)

[ " demo.py " ,&NBSP; Span style= "font-family: Arial" > ' one " ,&NBSP; " two " ,&NBSP; " three "

The Getopt module uses the Unix custom Getopt () function to perform sys.argv. in the Argparse The module provides many more powerful and flexible command-line operations.

7.4Error output redirection and program termination

The Sys module also contains many properties such as stdin,stdout , and stderr. The subsequent property pass is often used to throw warnings or error messages, and error messages can be seen when stdout redirects.

The most straightforward way to terminate a script is to use the sys.exit () method.

>>> sys.stderr.write ('Warning, log file not found starting a new one\n')

Warning, log file not found starting a new one

Python3.2 Official document Translation--Overview of the standard library (i)

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.