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

Steven D'Aprano steve at pearwood.info
Tue Sep 14 02:03:35 CEST 2010


On Tue, 14 Sep 2010 05:12:12 am Brian Jones wrote:
> Thanks for the replies so far. One thing that's probably relevant:
> once a directory is created, I can expect to write a couple of
> hundred files to it, so doing a 'try os.makedirs' right off the bat
> strikes me as coding for the *least* common case instead of the
> *most* common (which is that the directory exists and the file write
> succeeds). If this were a one-off file write.... well then things get
> easier, but I'd like to avoid attempting a makedirs 100 times when 99
> of those times I know it'll give me an error.

Why? Do you know how much time it will save? The extra scaffolding code, 
setting up the try...except block, takes time too. Unless you've 
profiled the code, you have no idea how wasteful 100 calls to makedirs 
will be, compared to the alternatives of:

look to see if the directory exists 100 times

or 

setting up 100 extra try...except blocks.

In any case, the time it takes to call makedirs() unnecessarily will 
probably be so small compared to the time you actually spending writing 
data to disk that you won't notice -- who cares if you reduce the time 
to save 100 files from 1.302 seconds to 1.293 seconds? But don't 
believe me, because I'm only guessing. Write both versions and time 
them with *realistic* amounts of data. If you normally write 100 five 
gigabyte files over a network share, why are we even having this 
conversation??? *wink*

Write for the reader (that would be you, in six months time) first, and 
then only optimize when you have to.


-- 
Steven D'Aprano


More information about the Tutor mailing list