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)