パイソン 2.7.3
コンソールから:
>>> try:
... fsock = open("something_that_does_not_exist")
... except IOError:
... print "The file does not exist"
... print "Let's keep going"
Traceback ( File "<interactive input>", line 5
print "Let's keep going"
^
SyntaxError: invalid syntax
同じコードをスクリプトに保存すると:
ex.py
def try1():
try:
fsock = open("something_that_does_not_exist")
except IOError:
print "The file does not exist"
print "Let's keep going"
そしてそれを実行します:
>>> import ex
>>> ex.try1()
The file does not exist
Let's keep going
>>>
コンソール、IDLE、および PythonWin でこれを試しました。同じ結果です。
違いは何ですか?
編集:
私は、他のソースの中でも、「Dive into Python」( http://www.diveintopython.net/ ) で Python を学んでいます。例 6.1 では、この例がコマンド ラインから実行されていることが示されてい ます。
だからこそ、これはうまくいくはずだと思いました。