0

I'm trying to create an exception based on whether or not a file has been removed:

def remove_file():
    print("Removing the output file: ", output_file)
    try:
        os.remove(output_file)
    except RemoveFileError,e:
        remove_stat = e.returncode
    if remove_stat == 0:
        print("File Removed!")

But I'm getting the following error:

  File ".\aws_ec2_list_instances.py", line 198
    except RemoveFileError,e:
                          ^
SyntaxError: invalid syntax

I thought I could call the exception whatever I wanted. Is that not the case? How can I write this correctly?

1
  • 2
    You used Python 2 syntax in Python 3. → except RemoveFileError as e: Commented Mar 21, 2019 at 15:44

1 Answer 1

1

Which version of Python are you using? Because the syntax has changed between Python 2 and Python 3.

In Python 2, it's (mostly) except Exception, var: where it is except Exception as var: in Python 3.

Notice that Python 3 syntax was backported to Python 2.6 and 2.7, so it is not unusual to see except Exception as var in those versions (and this should be preferred if you want this specific piece of code to work on both Python 2.6+ and 3+).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.