Can This Code Be Made Faster?

J

John Abel

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 ) ) ]

I'd appreciate any pointers.

John
 
G

Ganesan R

John" == John Abel said:
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
 
M

Michael Peuser

John Abel said:
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?


I think best help is with try - except (Ganesan already posted...)
But by all means NEVER use except: without any explicit exception; in this
case
except OSError:
would be appropriate.

If you by chance are using Windows only, a much better (and much faster)
suport will be given by
win32api.FindFiles
Of course this is not portable....

Kindly
Michael P
 
S

Sean Ross

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

======



def getFileTimes(fileList, rootPath):
# using a function can speed things up (I seem to recall)
fileTimes = []
# use aliases to save some look-up time inside loop
append = fileTimes.append; lstat = os.lstat; joinPath = os.path.join;
for fileName in fileList:
try:
append((lstat(joinPath(rootPath, fileName)).st_mtime, fileName))
except OSError:
pass
return fileTimes


I don't know if function composition would help speed (you'd have to
timeit.py),
but it might help readability:

# from "Python Cookbook", pg. 467
def compose(f, g, *orig_args, **orig_kwds):
def nested_function(*more_args, **more_kwds):
return f(g(*more_args, **more_kwds), *orig_args, **orig_kwds)
return nested_function

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


HTH
Sean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top