标准输出从输出到文件切换回输出到屏幕
先把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语句后再到外层进行捕获动作