473,763 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can This Code Be Made Faster?

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

Jul 18 '05 #1
3 1584
>>>>> "John" == John Abel <jo*******@pa.p ress.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.appen d(
(os.lstat(os.pa th.join(rootPat h, fileName)).st_m time, fileName)
)
except:
pass

======

Ganesan

--
Ganesan R
Jul 18 '05 #2

"John Abel" <jo*******@pa.p ress.net> schrieb im Newsbeitrag
news:ma******** *************** **********@pyth on.org...
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.FindFi les
Of course this is not portable....

Kindly
Michael P
Jul 18 '05 #3
"Ganesan R" <rg******@myrea lbox.com> wrote in message
news:ou******** ****@andlx-anamika.cisco.c om...
>> "John" == John Abel <jo*******@pa.p ress.net> writes:
[snip] ======
fileTimes = []
for fileName in fileList:
try:
fileTimes.appen d(
(os.lstat(os.pa th.join(rootPat h, fileName)).st_m time, fileName) )
except:
pass

======


def getFileTimes(fi leList, 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.appen d; lstat = os.lstat; joinPath = os.path.join;
for fileName in fileList:
try:
append((lstat(j oinPath(rootPat h, fileName)).st_m time, 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(f ileList, rootPath):
fileTimes = []
append = fileTimes.appen d
lstat = compose(os.lsta t, os.path.join)
for fileName in fileList:
try:
append( (lstat(rootPath , fileName).st_mt ime, fileName) )
except OSError:
pass
return fileTimes
HTH
Sean
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

242
13432
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any comments on past experience, research articles, comments on the matter would be much appreciated. I suspect something like C would be the best based on comments I received from the VB news group. Thanks for the help in advance James Cameron
354
15920
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets hooked up to interfaces. The Gtk is more than enough gui.
22
2109
by: Liang Chen | last post by:
Is the file "bcopy.c" in the "libitery" directory the implement of the GNU C library function "bcopy"? If so, how can it run so fast?(copy-by-byte rather than copy-by-word) When I copy the code of "libitery/bcopy.c" to my own code, I find that it is so slow even if I turn on "-O3" and define "NDEBUG". Why?
11
3767
by: hasadh | last post by:
Hi, is the assemly code for if..else and switch statements similar. I would like to know if switch also uses value comparison for each case internally or does it jump to the case directly at the assembly level ? for a performance critical application is it better to to use switch case or accomplish the same using fn pointers ?
14
3146
by: joshc | last post by:
I'm writing some C to be used in an embedded environment and the code needs to be optimized. I have a question about optimizing compilers in general. I'm using GCC for the workstation and Diab compiler for the embedded target. My question is about how compilers optimize certain code sequences. As an example, take the code below. Will the compiler eliminate the actual function call to foo() in the object code generated and just store...
11
1947
by: Eigenvector | last post by:
I apologize if this is a trivial question, but it's always made me wonder when I have to compile my code. There are some #includes that you don't really need to reference in your library and header references in the compilation string. For instance the standard hello.c #include <stdio.h> int main()
23
2528
by: Python Maniac | last post by:
I am new to Python however I would like some feedback from those who know more about Python than I do at this time. def scrambleLine(line): s = '' for c in line: s += chr(ord(c) | 0x80) return s def descrambleLine(line):
4
5245
by: Rick | last post by:
Access2003 in XP I'm using the code below to append any new records from (tbl_From_Mainframe) into (tbl_Appended_Data). It takes more than a minute to search 7000 records for a dozen new records. The file I'm searching is in a data farm so I'm stuck with using it in its present format. Linking to the file or importing it as a local table have no effect on speed. How can I get this code to perform quicker?
30
3544
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
1
1481
by: samoukos | last post by:
Hello i had to do this project but at school they tell me that it will be faster using stdio insteed of fstream... Is that right??? if it is faster can anyone suggest how this code will be using stdio???thank you???? here is the code: //Made by Samuel Johnson //email:sammojohn@gmail.com #include<iostream>
0
9566
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10149
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10003
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9943
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9828
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8825
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5410
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3529
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.