天天看點

python自定義異常

python自定義異常

try             異常在try塊裡抛,如果會産生多個異常,捕捉第一個,比對except,後邊的不再捕捉

except: 抓異常

else:       try無異常,才會執行else

finally:   無論try塊是否抛異常,永遠執行的代碼,通常用來執行關閉檔案,斷開伺服器連接配接的功能

[root@133 systeminformation]# vim ErrorExcept.py
#!/usr/bin/env python                                                                                                                                                              
#ecoding:utf-8                                                                                                                                                                     
                                                                                                                                                                                   
class FuncError(Exception):                                                                                                                                                        
    def __str__(self):                                                                                                                                                             
        return "I am func Error"                                                                                                                                                   
                                                                                                                                                                                   
def fun():                                                                                                                                                                         
    raise FuncError() #raise 抛出異常"I am func Error"                                                                                                                             
                                                                                                                                                                                   
try:                                                                                                                                                                               
    fun()                                                                                                                                                                          
except FuncError,e:                                                                                                                                                                
    print e                                                                                                                                                                        
                                                                                                                                                                                   
print 'hello world'   

[root@133 systeminformation]# python ErrorExcept.py 
I am func Error
hello world      
#!/usr/bin/env python                                                                                                                                                              
#ecoding:utf-8                                                                                                                                                                     

class FuncError(Exception):
    def __str__(self):
        return "I am func Error"

def fun():
    raise FuncError() #raise 抛出異常"I am func Error"

try:
   #fun()                                                                                                                                                           
    print  'a'         #print 'a'正确顯示結果,如果是print a,報錯name error,列印!!                                                                                                                                           
except FuncError,e:    #如果print a 和fun()同時存在,print a在前,會列印!!,不列印I am fun error, fun()在前,列印I am fun error,不列印!!                                                                                                                                        
    print e                                                                                                                                                       
except NameError:                                                                                                                                                 
    print '!!'                                                                                                                                                   
else:                  #不抛異常,輸出a,這種情況下執行else内容                                                                                                                                         
    print 'else'                                                                                                                                                  
finally:               #finally無論如何都執行                                                                                                                                         
    print 'finally'
print 'hello world'    #print一定會執行


[root@133 systeminformation]# python ErrorExcept.py                                                                                                                                
a
else
finally
hello world


#!/usr/bin/env python                                                                                                                                                              
#ecoding:utf-8                                                                                                                                                                     
                                                                                                                                                                                   
class FuncError(Exception):                                                                                                                                                        
    def __str__(self):                                                                                                                                                             
        return "I am func Error"                                                                                                                                                   
                                                                                                                                                                                   
def fun():                                                                                                                                                                         
    raise FuncError() #raise 抛出異常"I am func Error"                                                                                                                             
                                                                                                                                                                                   
try:                                                                                                                                                                               
   fun()               #即使有兩個異常,抛出一個異常I am func Error,不再抛出第二個                                                                                                                                                                   
   print  a                                                                                                                                                                  
except Exception:       #比對所有異常,比對即結束,列印all exception                                                                                                                                                          
    print 'all exception'                                                                                                                                                          
except FuncError,e:                                                                                                                                                                
    print e                                                                                                                                                                        
except NameError:                                                                                                                                                                  
    print '!!'                                                                                                                                                                     
else:                   #沒有異常菜執行else,有異常不執行                                                                                                                                                       
    print 'else'                                                                                                                                                                   
finally:                                                                                                                                                                           
    print 'finally'                                                                                                                                                                
print 'hello world'  

[root@133 systeminformation]# python ErrorExcept.py 
all exception
finally
hello world      

glob:python下類似shell中的*的通配符

In [1]: import glob                                                                                                                                                                
                                                                                                                                                                                   
In [2]: glob.glob('/etc/*.conf')    #比對/etc下*.conf檔案,儲存到list中                                                                                                                                              
Out[2]:                                                                                                                                                                            
['/etc/rsyslog.conf',
 '/etc/sensors3.conf',
 '/etc/GeoIP.conf',
 '/etc/Trolltech.conf',
 '/etc/nfsmount.conf',      
[root@133 ~]# ps -eo pid,ppid,cmd                                                                                                                                            
  PID  PPID CMD
    1     0 /sbin/init
    2     0 [kthreadd]
    3     2 [migration/0]
    4     2 [ksoftirqd/0]

[root@133 ~]# ipython
In [1]: import shlex                                                                                                                                                               
                                                                                                                                                                                   
In [2]: cmd = "ps -eo pid,ppid,cmd"                                                                                                                                                
                                                                                                                                                                                   
In [3]: shlex.split(cmd)              #傳回清單元素                                                                                                                                              
Out[3]: ['ps', '-eo', 'pid,ppid,cmd']                                                                                                                                              

In [4]: import subprocess                                                                                                                                                          
                                                                                                                                                                                   
In [5]: subprocess.check_call(shlex.split(cmd))                                                                                                                                    
  PID  PPID CMD                                                                                                                                                                    
    1     0 /sbin/init                                                                                                                                                             
    2     0 [kthreadd]                                                                                                                                                             
    3     2 [migration/0]