python except 学习
#!/usr/bin/env python
"""test module, Uesd to learn try and os module"""
import os
import sys
ls = os.linesep
while True:
filename = raw_input("src file name>")
try:
src = open(filename, 'r')
break; #can't get here if input wrong name
except IOError:
print "IO error, input again" #catch IOError when input path not exists
while True:
filename = raw_input("dst file name>")
if os.path.exists(filename): #os module check if the file exists
dst = open(filename, 'a')
break;
else:
print "input again"
print ("%s>%s" % (src, dst))
dst.writelines('%s%s' % ("start add", ls) )
for eachline in src:
dst.writelines('%s' % eachline)
dst.close()
src.close()
print "DONE!"