标签:style blog http color io os ar strong 文件
最常用的两个模块:
[root@likun python_scripts]# cat 10sys.py #!/usr/bin/python import sys print sys.argv print sys.argv[1] print sys.argv[1:3]
[root@likun python_scripts]# python 10sys.py aaa bbb ccc ddd [‘10sys.py‘,‘aaa‘,‘bbb‘,‘ccc‘,‘ddd‘] aaa [‘aaa‘,‘bbb‘]
>>> import os >>> os.system(‘hostname‘) 执行系统命令 likun.com 0 执行成功则返回0 >>> os.system(‘mkdir books‘) 0 >>> os.system(‘mkdir %s‘ % col) 也可以带入变量 0 >>> os.system(‘pwd && hostname‘) 执行多条系统命令 /root/python_scripts likun.com 0 >>> os.system(‘pwd ; hostname‘) /root/python_scripts likun.com 0 >>> os.chdir(‘/tmp‘) 改变目录。(用os.system(cd /tmp)下一条命令会失效) >>> os.listdir(‘/home‘) 将目录中内容存入列表中 [‘oracle‘, ‘nagios‘] >>> os.listdir(‘/home‘)[0] ‘oracle‘
#!/usr/bin/python name =‘My mod‘ version =‘2.1‘ askname=raw_input(‘Please input your name:‘)
>>>import mod --导入模块即执行模块
Please input your name:lk
>>> mod.askname
‘l k‘
>>> mod.version
‘2.1‘
>>> print mod.version
2.1
>>> mod.name
‘My mod‘
#!/usr/bin/python name =‘My mod‘ version =‘2.1‘ def askname(): askname=raw_input(‘Please input your name:‘) return askname
>>>import mod
>>> mod.askname()
Please input your name:good
‘good‘
[root@likun python_scripts]# cat service.py
#!/usr/bin/python import sys import os if len(sys.argv)<3: print ‘usage: mysql {status|start|stop|restart}‘ sys.exit() if sys.argv[1]==‘mysql‘: action=sys.argv[2] os.system(‘/mysql/support-files/mysql.server %s‘%action)
标签:style blog http color io os ar strong 文件
原文地址:http://www.cnblogs.com/kissdb/p/4009583.html