6. Python Module

Source: Internet
Author: User

The two most commonly used modules:

Os#可以允许python调用执行系统命令, such as the shellSYS#处理与python程序本身的事情 Python comes with more than 200 common modulesThe Python website collects more than 2000 modules and can basically find any features you want, including graphics and interfaces .  SYS module>>>Import SYS        Import Module>>>Sys.path    List the directories for the search modulesys.argv: Output parameter when invoking script
  1. [[email protected] python_scripts] #  #!/usr/bin/pythonimport  sysprint   sys.argv  Print sys.argv[1]print Sys.argv[1:3]

  2. [[email protected] python_scripts]#python 10sys.py AAA BBB CCC DDD['10sys.py','AAA','BBB','CCC','DDD']aaa['AAA','BBB']

>>>sys.exit ()                exiting the current Python program  OS module
>>>ImportOS>>> Os.system ('hostname') Execute system command LIKUN.COM0 Successful execution returns 0>>> Os.system ('mkdir Books') 0>>> Os.system ('mkdir%s'%col) can also be brought into the variable 0>>> Os.system ('PWD && hostname') executes multiple system commands/root/Python_scriptslikun.com0>>> Os.system ('pwd; hostname')/root/Python_scriptslikun.com0>>> Os.chdir ('/ tmp') to change the directory. (with Os.system (CD/tmp) The next command will be invalidated)>>> Os.listdir (' /Home') to save the contents of the catalog in the list ['Oracle','Nagios']>>> Os.listdir (' /Home') [0]'Oracle'

 Time ModuleTime.sleep (2)  Production Module
 1. Create a mod.py file in the current directory
    1.  Span style= "color: #008000;" >#  !/usr/bin/python  name = " my mod   " version  = '  
  1. >>>import mod                            --导入模块即执行模块
  2. Please input your name:lk
  3. >>> mod.askname
  4. ‘l k‘
  5. >>> mod.version
  6. ‘2.1‘
  7. >>> print mod.version
  8. 2.1
  9. >>> mod.name
  10. ‘My mod‘
 if the custom module is not in the current directory, you need to add the directory that contains the module to the SYS.PATH environment variable:program exits will expire>>> Import sys>>> sys.path.append ('/root/python_scripts/') >>> import mod  2, import the module is not enabled, you need to put the module contents into the function
    1.  Span style= "color: #008000;" >#  !/usr/bin/python  name = " my mod   " version  = '  
    1. >>> mod.askname()
    2. Please input your name:good
    3. ‘good‘
little Exercise: Make a program similar to service XXX startVI service.py[[email protected] support-files]# python service.py httpd status  Small Exercise: make a non-canonical service startup script:
  1. [[email protected] python_scripts]# cat service.py 
  2. #!/usr/bin/pythonImportSYSImportOSifLen (SYS.ARGV) <3:        Print 'usage:mysql {Status|start|stop|restart}'sys.exit ()ifsys.argv[1]=='MySQL': Action=sys.argv[2] Os.system ('/mysql/support-files/mysql.server%s'%action)

[[email protected] python_scripts]# python service.py mysql restartshutting down MySQL. [OK]starting MySQL. [OK] 



6. Python 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.