python-異常
標準輸出從輸出到文件切換回輸出到屏幕
先把sys.stdout保存到變量裏,然後再切換回來
>>> import sys
>>> saveStdout=sys.stdout
>>> print "hello world"
>>> print "glory road"
>>> saveStdout
>>> sys.stdout=saveStdout
>>> print "hello"
Hello
Shutil模塊
>>> os.chdir("d:\\dir")
>>> shutil.copy("d:\\xx.txt","d:\\dir\\")
>>> os.listdir("d:\\dir")
[‘xx.txt‘]
>>>
拷貝目錄
>>> shutil.copytree("d:\\dir","d:\\test")
>>> os.chdir("d:\\test")
>>> os.listdir(‘.‘)
[‘xx.txt‘]
>>> os.getcwd()
‘d:\\test‘
>>>
異常
#coding=utf-8
print "1"
try:
a
except:
print "error occur"
print "2"
.
Try:
<語句>#可能發生異常的代碼
Except<名字>
<語句>#如果在try部分引發了名字異常,則執行這裏的代碼
Except<名字2>
<語句>#如果在try部分引發了名字2異常,則執行這裏的代碼
Else:
<語句>#如果沒有引發異常,則執行這裏的代碼
將可能發引發異常的語句,放到try語句快中,讓except語句捕獲異常並處理
#coding=utf-8
print "1"
try:
a
except IOError:
print "IOError occur"
except NameError:
print "NameError occur"
else:
print "no error"
print "2"
出發IOerror,
#coding=utf-8
print "1"
try:
open("d:xxxxx.py")
except IOError:
print "IOError occur"
except NameError:
print "NameError occur"
else:
print "no error"
print "2"
Except語句沒有捕捉到異常,則會報錯
#coding=utf-8
print "1"
try:
1/0
except IOError:
print "IOError occur"
except NameError:
print "NameError occur"
else:
print "no error"
print "2"
不帶參數的except語句放到最後,可以捕獲前邊except語句沒有捕獲的所有異常。
#coding=utf-8
print "1"
try:
1/0
except IOError:
print "IOError occur"
except NameError:
print "NameError occur"
except:
print "unkown error occur"
else:
print "no error"
print "2"
Try中有多個異常時,系統只會處理第一個,就跳出try語句了
#coding=utf-8
print "1"
try:
a
open("d:\\ssss.py")
except IOError:
print "IOError occur"
except NameError:
print "NameError occur"
except:
print "unkown error occur"
else:
print "no error"
print "2"
except (IOError,NameError):except同時處理幾個異常
#coding=utf-8
print "1"
try:
a
except (IOError,NameError):
print "IOError occur or NameError occur"
except:
print "unkown error occur"
else:
print "no error"
print "2"
文件不存在或者讀寫有問題,都會拋IOError
#coding=utf-8
print "1"
try:
fp=open("d:\\file.txt",‘r‘)
fp.write("test")
fp.close()
except IOError:
print u"文件寫入失敗!"
else:
print u"文件寫入成功"
讓用戶輸入整數,如果不是就報錯
>>> try:
... num=int(raw_input("input a interger:"))
... except:
... print "wrong number formata"
...
input a interger:3.2
wrong number formata
練習,死循環讓用戶輸入整數,如果輸入的是整數就推出
#coding=utf-8
while True:
try:
num=int(raw_input("input a interger:"))
break
except:
print "input wrong number format"
或者
#coding=utf-8
while True:
try:
num=int(raw_input("input a interger:"))
except:
print "input wrong number format"
else:
break
print num
traceback,print traceback.format_exc()出現錯誤時,把出錯的行的堆棧信息打印出來,程序繼續執行
#coding=utf-8
import traceback
while True:
try:
num=int(raw_input("input a interger:"))
except:
print "input wrong number format"
print traceback.format_exc()
else:
break
print num
>>> import traceback
>>> try:
... a
... except:
... print traceback.format_exc()
... print "traceback.format.exe()"
... print "traceback.format.exe()"
... print "traceback.format.exc()"
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
NameError: name ‘a‘ is not defined
traceback.format.exe()
traceback.format.exe()
traceback.format.exc()
捕獲異常可以嵌套
#coding=utf-8
import traceback
while True:
try:
try:
num=int(raw_input("input a interger:"))
except:
print "input wrong number format"
1/0
except:
print traceback.format_exc()
print "inner 1/0 error comes to out try:"
else:
break
print num
死循環:
#coding=utf-8
import traceback
while True:
try:
try:
num=int(raw_input("input a interger:"))
except:
print "input wrong number format"
1/0#無論輸入的值是正確都會走這裏,並出發異常
except:#因為前面的異常,所以代碼肯定會走這裏
print traceback.format_exc()
print "inner 1/0 error comes to out try:"
else:#因為代碼走了前面的except語句,就不會走else語句,所以這裏被短路了
print "here"
break
print num
#coding=utf-8
import traceback
while True:
try:
try:
num=int(raw_input("input a interger:"))
except:
print "input wrong number format"
1/0#當代碼走到這裏觸發異常時,不會被捕獲,所以直接退出
except IOError:
print traceback.format_exc()
print "inner 1/0 error comes to out try:"
else:
print "here"
break
print num
多個except時,攔截到一個,後邊的不會執行
Finally語句不管有沒有異常,都會執行
#coding=utf-8
import traceback
try:
1/1#不會觸發異常
except:
print "error occur"
finally:#應該走這裏
print "done!"
#coding=utf-8
import traceback
try:
1/0#報異常
except:
print "error occur"
finally:#也會走這裏
print "done!"
File.closed()判斷文件是否關閉。
>>> fp.mode
‘w‘
>>> fp.closed
False
>>> fp.name
‘d:\\a.txt‘
>>> fp.softspace
0
>>>
file.closed 返回true如果文件已被關閉,否則返回false。
file.mode 返回被打開文件的訪問模式。
file.name
返回文件的名稱。
file.softspace 如果用print輸出後,是否跟一個空格符, false
操作文件的話,在finally裏關掉文件
import traceback
try:
fp=open("d:\\1111111111111.txt","w")
fp.write("haha")
except Exception,e:#e是消息
print e
finally:
if not fp.closed:
fp.close( )
except Exception,e:#這裏的Exception是關鍵字,e是指消息(可變),這樣可以操作消息
#coding=utf-8
import traceback
import traceback
try:
fp=open("c:\\1111111111111.txt","w")
fp.write("haha")
except Exception,a:#e是消息
print a
print "exceptino!@!@@@@@"
tracetack.format_exc()和except Exception as msg:print msg或者except Exception,e: print e是一樣的
#coding=utf-8
import traceback
try:
fp=open("c:\\1111111111111.txt","w")
fp.write("haha")
except Exception,e:
print e
print "exceptino!@!@@@@@"
用try finally關閉文件
#coding=utf-8
try:
fp=open("d:\\xxx.txt",‘r‘)
try:
content=fp.read()
print content
finally:
print u"關閉文件"
fp.close()
except IOError:
print u"Error:沒有找到文件或讀取文件失敗
#coding=utf-8
try:
fp=open("e\\xxx.txt",‘r‘)#不存在
try:
content=fp.read()
print content
finally:
print u"關閉文件"
fp.close()
except IOError:
print u"Error:沒有找到文件或讀取文件失敗"
Else 和except都要放到finally前邊
Raise IOError手動拋出異常
#coding=utf-8
try:
fp=open("d:\\xxx.txt",‘r‘)
try:
print "1"
raise IOError
finally:
print u"關閉文件"
fp.close()
except IOError:
print u"Error:沒有找到文件或讀取文件失敗"
Raise IOError手動拋出異常
#coding=utf-8
try:
fp=open("e:\\xxx.txt",‘r‘)
try:
print "1"
raise IOError
finally:
print u"關閉文件"
fp.close()
except Exception,e:
print u"文件讀取失敗"
print u"打印異常信息"
print e
#coding=utf-8
try:
fp=open("d:\\xxx.txt",‘r‘)
try:
print "1"
raise IOError
finally:
print u"關閉文件"
fp.close()
except Exception,e:
print u"文件讀取失敗"
print u"打印異常信息"
print e
finally:
print "finally"
一層try模塊中有異常,先不進行外層的捕獲動作,而是先執行本層finally語句後再到外層進行捕獲動作
python-異常