[Tutor] What's the best way to ask forgiveness here?

Emile van Sebille emile at fenx.com
Mon Sep 13 20:56:01 CEST 2010


On 9/13/2010 11:31 AM Brian Jones said...
> I've been coding Python long enough that 'asking forgiveness instead of
> permission' is my first instinct, but the resulting code is sometimes
> clumsy, and I wonder if someone can suggest something I'm missing, or at
> least validate what's going on here in some way.
>
> What I'm trying to do is write a file to a directory. However, the directory
> may not exist the first time I try to write a file there, so I'm going to
> first try to write the file, and if I get an exception, create the directory
> (er, *try* to), and *then* write the file there. Here's my first shot at the
> code:
>
>          try:
>              self.save_file(picfile_fullpath, picdata)
>          except IOError as err:
>              # directory doesn't exist. Try to create it.
>              try:
>                  os.makedirs(picfile_fullpath)
>              except OSError as oserr:
>                  logging.error("Can't create file path: %s (%s)" %
> (picfile_fullpath, oserr))
>              else:
>                  # Created dir, now write file.
>                  try:
>                      self.save_file(picfile_fullpath, picdata)
>                  except IOError as err:
>                      logging.error("Bailing. Couldn't save file %s (%s)" %
> (picfile_fullpath, err))
>                      return False

Unless this is in a tight loop, I think I'd write:

try:
     os.makedirs(picfile_fullpath)
     try:
         self.save_file(picfile_fullpath, picdata)
         return True
         # all/only error handling code follows.
     except IOError as err:
         logging.error("Bailing. Couldn't save file %s (%s)" % 
(picfile_fullpath, err))
         return False
except OSError as oserr:
     logging.error("Can't create file path: %s (%s)" % 
(picfile_fullpath, oserr))
     return False

which eliminates the duplicated save_file step.

>
> Doesn't this seem less readable than the 'ask permission' equivalent? I
> think it does, but in this case asking permission for every single operation
> when the dir will only need to be created a single time (and then may be
> written to several hundred times) is pretty wasteful.

Agreed -- if it's in a loop, you'd want to only check once.  Of course, 
if the directory is eg an nfs share, continued access could be an issue.

HTH,

Emile


>
> I suppose I could set some sentinel variable and check for it in a while
> loop, but then I need some other scaffolding code to make sure I don't
> infinitely loop trying to create the directory, and probably some other
> stuff I'm forgetting, so it strikes me as being just as messy.
>
> Is there a clean sort of pattern to apply in instances like this?
>
> Thanks.
> brian
>
>
>
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list