optomizations

Chris Angelico rosuav at gmail.com
Tue Apr 23 00:08:33 EDT 2013


On Tue, Apr 23, 2013 at 2:00 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> Also, you're searching the file system *for every line* in the input
> file. Pull this outside of the loop and have it run once:
>
>                     if not os.path.exists(outdir):
>                         os.makedirs(outdir)
>
> Likewise for opening and closing the output file, which you currently
> open and close it for every line. It only needs to be opened and closed
> once.

The outdir depends on the line, though. Hence my suggestion to retain
the open files in a dictionary.

ChrisA



More information about the Python-list mailing list