Python Module initial knowledge

Source: Internet
Author: User

The power of Python is that it has a rich set of standard libraries and third-party libraries for various functions, and allows users to build their own library files.

Standard modules (also known as libraries) include SYS, OS, glob, Sockets, threading, _thread, queue, time, Timeit, subprocess, multiprocessing, signal, select, s Hutil, Tempfile and so on.

Most system-level interfaces focus on: SYS and OS two modules.

The SYS and OS modules are briefly described below:

SYS module

Platform and version information, such as Sys.platform, Sys.maxsize, sys.version
Module Search Path Sys.path
Module table Sys.modules, which is a dictionary containing the Name:module information for the import module in the Python program
Exception information, such as Sys.exc_info ()
Command-line Arguments SYS.ARGV
Standard flow, such as Sys.stdin, Sys.stdout, Sys.stderr
Program exits call Sys.exit

1 #!/usr/bin/env python2 #file_name:test_sys.py3 4 ImportSys#Import Module5 Print(Sys.path)#Output Module Search path6 7 Print(SYS.ARGV)#SYS.ARGV implements parameters that receive external passes, including file names8 Print(Sys.argv[0])#the name of the script always sys.argv the first parameter of the list, which is argv[0]9 Print(Sys.argv[1])#other parameters are argv[1]Ten Print(Sys.argv[2])#Argv[2] One Print(Sys.argv[3])#Argv[3]

Terminal execution:

Python test_sys.py I Love python

Run results

[' E:\\vscode_pragram\\python3\\python base \\day2\\ code ', ' C:\\software\\python\\python35-32\\python35.zip ', ' C:\\ Software\\python\\python35-32\\dlls ', ' c:\\software\\python\\python35-32\\lib ', ' c:\\software\\python\\ Python35-32 ', ' c:\\software\\python\\python35-32\\lib\\site-packages ' [' sys_test.py ', ' I ', ' love ', ' Python '] Sys_test.pyilovepython

OS Module

Python The OS module contains common operating system features. This module is especially important if you want your program to be platform-agnostic. If we want to manipulate files and directories, we can enter various commands provided by the operating system under the command line to complete. such as DIR, CP and other commands. In fact, the command provided by the operating system simply invokes the interface functions provided by the operating system, and Python's built-in OS module can also directly invoke the interface functions provided by the operating system.

 #  !/usr/bin/env python  #   file_name:os_test.py   Import   Osos.system (  " dir  " ) #   #   This method cannot be saved with a variable, and immediately printed  
when called
# plan to use Dir_req to save the listed file name information, but the result is not so, see dir_req = Os.system ("dir")  Print("--->", Dir_req)

Terminal output:

the volume in drive E is not labeled. The serial number of the volume is 000D-4517 e:\vscode_pragram\Python3\Python base \day2\ code 2017/04/15  09:51    <DIR>           . 2017/04/15  09:51    <DIR>           . 2017/04/15  09:58               184 os_test.py2017/04/15  09:44               2 sys_test.py                Files            364               bytes 2 directories 60,183,629,824 Available Bytes ---> 0
Finally, the output of the--->0, indicating that Dir_req is 0, that is, when the Os.system ("dir") run successfully returned 0, unsuccessful return error code if you need to save information, the Popen method will be used as follows:
# !/usr/bin/env python # file_name:os_test2.pydir_req = Os.popen ("dir")   #  dir_req Returns a file description symbol for the open file object of FD print (dir_req)              #  Prints the file descriptor to screen print( " \ n " )                 print(Dir_req.read ())       #  reads the contents of the file descriptor using the Read method

Terminal output:

<os._wrap_close object at 0x01b1b7b0> Drive E does not have a label on the volume. The serial number of the volume is 000D-4517 e:\vscode_pragram\mine\Python3\Python base \day2\ code 2017/04/15  09:51    < Dir>          . 2017/04/15  09:51    <DIR>           . 2017/04/15  10:03               269 os_test.py2017/04/15  09:44               2 sys_test.py                Files            449 bytes                2 directories 60,183,629,824 bytes available

---------------------------------------------------------------------------------------

Python sys module all Methods (English): https://docs.python.org/3/library/sys.html

Python OS module all methods: http://www.runoob.com/python/os-file-methods.html

Os.popen Method: http://www.runoob.com/python/os-popen.html

Python Module initial knowledge

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.