Python异常处理IOError

时间:2014-04-11 20:28:39

标签: python exception directory

我试图编写一个简单的pything代码,试图打开一个不存在的文件,但它会处理异常购买显示"无法打开,没有这样的目录或文件"关于我做错的事情的任何建议?

import sys

try:

    f = open("h:\\nosuchdirectory\\nosuchfile.txt","w") # do not modify this line

except IOError:
    print('Cannot open, no such directory or file')

f.write("This is the grape file") # do not modify this line

f.close() # do not modify this line

我开始使用的最初代码是:

f = open("h:\\nosuchdirectory\\nosuchfile.txt","w" # do not modify this line

f.write("This is the grape file") # do not modify this line

f.close() # do not modify this line

0 个答案:

没有答案