Can This Code Be Made Faster?

Ganesan R rganesan at myrealbox.com
Fri Sep 12 06:21:03 EDT 2003


>>>>> "John" == John Abel <john.abel at pa.press.net> writes:

> I have a problem, in that the following code is sometimes failing.  I
> am stat'ing a list of files, but, sometimes, one of the files may be
> moved by an external process, after the os.path.exists and before the
> lstat, causing the script to fail.  My question, is there anything I
> can do to speed it up, or put in a trap, to stop the stat from failing?
>         fileTimes = [ ( os.lstat( os.path.join( rootPath, fileName )
> )[stat.ST_MTIME], fileName) for fileName in fileList \
>                                                                             if
> os.path.exists( os.path.join( rootPath, fileName ) ) ]

    
Then there's no point in doing os.path.exists(), you might as well call
os.lstat directly. Some like this should work:

======
fileTimes = []
for fileName in fileList:
    try:
        fileTimes.append(
            (os.lstat(os.path.join(rootPath, fileName)).st_mtime, fileName)
        )
    except:
        pass

======

Ganesan

-- 
Ganesan R




More information about the Python-list mailing list