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
[[email protected] python_scripts] # #!/usr/bin/pythonimport sysprint sys.argv Print sys.argv[1]print Sys.argv[1:3]
[[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
-
Span style= "color: #008000;" ># !/usr/bin/python name = " my mod " version = '
>>>import mod --导入模块即执行模块
Please input your name:lk
>>> mod.askname
‘l k‘
>>> mod.version
‘2.1‘
>>> print mod.version
2.1
>>> mod.name
‘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
-
Span style= "color: #008000;" ># !/usr/bin/python name = " my mod " version = '
>>> mod.askname()
Please input your name:good
‘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:
[[email protected] python_scripts]# cat service.py
#!/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