Trying first is the best way, I recommend doing this.
try: open(filename, 'w') except OSError: # handle error here
I believe that you will get an OSError, catch it explicitly and test on the platform you are using it on.
Jerub Nov 05 '10 at 1:41
source share