一、函数
1、函数定义示例
[root@saltstack python]# cat func1.py #!/usr/bin/env python def hello(): name=raw_input('Enter your name: ') print "Hello %s, welcome to china!" % name if name == 'hh': print '%s is chinese!' % name hello() [root@saltstack python]# python func1.py Enter your name: qq Hello qq, welcome to china! [root@saltstack python]# python func1.py Enter your name: hh Hello hh, welcome to china! hh is chinese!
2、函数传递参数
[root@saltstack python]# cat func2.py #!/usr/bin/env python a=raw_input('Enter a number: ') b=raw_input('Enter a number: ') def printMax(a,b): if a > b: print a,'is max num!' else: print b,'is max num!' printMax(a,b) x=5 y=7 printMax(x,y) [root@saltstack python]# python func2.py Enter a number: 33 Enter a number: 44 44 is max num! 7 is max num!
二、Python基础模块
1、sys和os模块
# 创建目录 >>> os.mkdir('/tmp/1111') # 删除 >>> os.rmdir('/tmp/1111') # 调用系统命令 >>> os.system('ifconfig eth0') eth0 Link encap:Ethernet HWaddr 00:0C:29:BD:50:EC inet addr:10.0.0.10 Bcast:10.0.0.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:febd:50ec/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:377505 errors:0 dropped:0 overruns:0 frame:0 TX packets:311216 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:111653102 (106.4 MiB) TX bytes:226744867 (216.2 MiB) 0 >>> os.system(''' ifconfig eth0 | awk -F'[ :]+' '/inet addr/{print $4}' ''') 10.0.0.10 0 >>> ip=os.popen(''' ifconfig eth0 | awk -F'[ :]+' '/inet addr/{print $4}' ''').read() >>> print ip 10.0.0.10 >>> os.system('ls /root') 123.conf 22.sh count.sh for.sh install.log mmmm.txt shell users.sh 1.txt anaconda-ks.cfg disk.tmp hm install.log.syslog nest.sh syscheck.txt 20.0.0.20 backup env.sh hm_suse_system_check.sh ip.sh on.sh sys.sh 2222.sh check_load.sh file.txt hunan_10.0.0.31.txt load.sh python t.sh 0
2、调用模块的子模块
#!/usr/bin/env python from os import mkdir,system print '----------------' mkdir('/tmp/123') #直接引用 system('ls /tmp/') #直接引用 print '----------------' print 'finish.' [root@saltstack python]# python modules.py ---------------- 123 file.txt logs2 yum_save_tx-2016-07-15-00-42YkDlbJ.yumtx 1.txt hsperfdata_root mem_top.log yum_save_tx-2016-07-20-01-13cvenZA.yumtx cpu_top.tmp logs mem_top.tmp ---------------- finish.
3、tab补齐模块
>>> import readline >>> import rlcompleter >>> readline.parse_and_bind('tab: complete') >>> import tab
# tab补齐引用脚本,将下面写好的脚本放到python目录/usr/lib64/python2.6/下 [root@saltstack python]# cat tab.py #!/usr/bin/env python # python startup file import sys import readline import rlcompleter import atexit import os # tab completion readline.parse_and_bind('tab: complete') # history file histfile = os.path.join(os.environ['HOME'], '.pythonhistory') try: readline.read_history_file(histfile) except IOError: pass atexit.register(readline.write_history_file, histfile) del os, histfile, readline, rlcompleter # 导入tab模块 >>> import os >>> import sys >>> sys.path ['', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib64/python2.6/site-packages/gtk-2.0', '/usr/lib/python2.6/site-packages', '/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info'] >>> import tab >>> os._ os._Environ os.__getattribute__( os.__repr__( os._exit( os.__all__ os.__hash__( os.__setattr__( os._get_exports_list( os.__class__( os.__init__( os.__sizeof__( os._make_stat_result( os.__delattr__( os.__name__ os.__str__( os._make_statvfs_result( os.__dict__ os.__new__( os.__subclasshook__( os._pickle_stat_result( os.__doc__ os.__package__ os._copy_reg os._pickle_statvfs_result( os.__file__ os.__reduce__( os._execvpe( os._spawnvef( os.__format__( os.__reduce_ex__( os._exists(
三、自定义模块
1、定义一个模块
[root@saltstack python]# cat mymodule1.py #!/usr/bin/env python def hello(): name = raw_input('Enter your name: ') print "Hi %s, This is a python module." % name hello()
2、引用自定义的模块
[root@saltstack python]# cat demo1.py #!/usr/bin/env python import mymodule1 # 自定义模块的名称,也就是模块脚本的名称 print "The module name is version 1.1" [root@saltstack python]# python demo1.py Enter your name: hh Hi hh, This is a python module. The module name is version 1.1