peterphonic
peterphonic

Reputation: 1039

Catch exception with python under windows and UNIX

I got some code that i did 3 years ago under python 2.5 or so. It was working properly back then It was something like this :

try:
   if os.path.exists(os.path.join(TRACES.log_file_path, TRACES.log_file_name)):
      os.remove(os.path.join(TRACES.log_file_path, TRACES.log_file_name))
except IOError , e:
   print (str(e))

Now, under 3.2, this piece of code is not working for windows. I get the following error : Invalid syntax

To resolve the problem, i changed the "," for "as" to have the following :

try:
  if os.path.exists(os.path.join(TRACES.log_file_path, TRACES.log_file_name)):
    os.remove(os.path.join(TRACES.log_file_path, TRACES.log_file_name))
except IOError as e:
  print (str(e))

I was happy, but couple minutes later, i realized that the code was not working into a cygwin prompt, the compiler didn't like the "as", i had to switch back to a comma!

I would like to know what is the exact syntax of try except? I would like to have the same syntax for windows, unix and mac os

Thank you

Upvotes: 0

Views: 172

Answers (1)

Russell Borogove
Russell Borogove

Reputation: 19037

The operating system shouldn't make a difference to Python syntax. Most likely, your cygwin installation includes a Python 2.5. (Confirm this by just typing 'python' in your cygwin prompt and checking the banner.)

Note that 'a cygwin prompt' is not Unix.

If you must, install Python 3 under Cygwin.

Upvotes: 2

Related Questions