473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Calling a definition

I have a piece of code that I need some help with. It is supposed (in
my mind at least) take two arguments, a start path and a file
extension. Then when called it should return each of the file paths
that are found matching the criteria. It is only returning the first
file that it finds. What am I doing wrong?

# this is in a file called findFile.py
def findFileExt(startPath, fileExt):
for root, dirs, files in os.walk(startPath):
for file in files:
if file.endswith(fileExt):
filePath = str(os.path.join(root, file))
return filePath

# this part is in a different file calling the findFile module
ip_list = findFile.getIpRange(net, start, end)

for ip in ip_list:
src_path = '\\\\%s\\%s\\' % (ip, start_dir)
files = findFile.findFileExt(src_path, ext)
print files

Oct 19 '06 #1
4 1065
I have a piece of code that I need some help with. It is
supposed (in my mind at least) take two arguments, a start
path and a file extension. Then when called it should return
each of the file paths that are found matching the criteria.
It is only returning the first file that it finds. What am I
doing wrong?

1 def findFileExt(startPath, fileExt):
2 for root, dirs, files in os.walk(startPath):
3 for file in files:
4 if file.endswith(fileExt):
5 filePath = str(os.path.join(root, file))
6 return filePath

On line 7, you return from the function which prevents the
remainder of the code in the function/loop from being
processed.

You'd either have go gather them all in a list and then
return that gathered list:

1 def findFileExt(startPath, fileExt):
+ results = []
2 for root, dirs, files in os.walk(startPath):
3 for file in files:
4 if file.endswith(fileExt):
5 filePath = str(os.path.join(root, file))
-6 return filePath
+ results.append(filePath)
+ return results

or, you could write it as a generator:

1 def findFileExt(startPath, fileExt):
2 for root, dirs, files in os.walk(startPath):
3 for file in files:
4 if file.endswith(fileExt):
5 filePath = str(os.path.join(root, file))
-7 return filePath
+ yield filePath

which can then be used like

for thing in findFileExt(src_path, ext):
do_something(thing)

-tkc


Oct 19 '06 #2
"elake" (if that's supposed to be swedish, that should be "elak") wrote:
I have a piece of code that I need some help with. It is supposed (in
my mind at least) take two arguments, a start path and a file
extension. Then when called it should return each of the file paths
that are found matching the criteria. It is only returning the first
file that it finds. What am I doing wrong?
you can only return from a function once for each call.
# this is in a file called findFile.py
def findFileExt(startPath, fileExt):
for root, dirs, files in os.walk(startPath):
for file in files:
if file.endswith(fileExt):
filePath = str(os.path.join(root, file))
return filePath
and here you're doing exactly that.

if you expect to be able to loop over the return value from findFileExt,
replace that "return" with a "yield" (this turns the function into a
generator).

if you want to return a sequence, change the loop so it appends stuff to
a list, and return that list when you're done.

</F>

Oct 19 '06 #3
At Thursday 19/10/2006 15:43, elake wrote:
>I have a piece of code that I need some help with. It is supposed (in
my mind at least) take two arguments, a start path and a file
extension. Then when called it should return each of the file paths
that are found matching the criteria. It is only returning the first
file that it finds. What am I doing wrong?
Someone else has shown how to make your code work. But notice that
you don't even need the findFileExt function: see the glob module.
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Oct 19 '06 #4
Thanks for all of the help guys. I am still new to Python so this is
part of the learning curve I guess. I will look at the glob module and
see if that will do what I need to better.

On Oct 19, 3:34 pm, Gabriel Genellina <gagsl...@yahoo.com.arwrote:
At Thursday 19/10/2006 15:43, elake wrote:
I have a piece of code that I need some help with. It is supposed (in
my mind at least) take two arguments, a start path and a file
extension. Then when called it should return each of the file paths
that are found matching the criteria. It is only returning the first
file that it finds. What am I doing wrong?Someone else has shown how to make your code work. But notice that
you don't even need the findFileExt function: see the glob module.

--
Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!http://www.yahoo.com.ar/respuestas
Oct 19 '06 #5

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

Similar topics

5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
1
by: Asapi | last post by:
1. Are linkage convention and calling convention referring to the same thing? 2. Does calling convention differ between languages C and C++? 3. How does calling convention differ between...
19
by: Deniz Bahar | last post by:
Hi, I would like to call one of my functions the exact name as an existing C library function (for example K&R2 exercises asks me to make an atof function). If I don't include the header with...
1
by: Paul Brun | last post by:
Hi guys, I get the following error during runtime: Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at SXInit(Int32 ) The SXInit...
1
by: Vijay | last post by:
Hi, I do have the VB.NET method which has delegate as parameter. I am calling this method from VB.NET project by using the commnad "Addressof( Function name which has same signature)". How can i...
5
by: Ben | last post by:
This is a follow-on from the "Help with array/pointer segmentation fault needed" thread.. I have not been able to find much information on the rules for macros. I want to be able to call a macro...
16
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
4
by: Dan | last post by:
Hi All, I've got a problem with my C++ application that calls a Java class that I've built with GCJ, I can't run it because I get errors: multiple definition of `atexit' first defined here...
10
by: sulekhasweety | last post by:
Hi, the following is the definition for calling convention ,which I have seen in a text book, can anyone give a more detailed explanation in terms of ANSI - C "the requirements that a...
1
by: Martin Rubey | last post by:
Dear all, I'm trying to call from common lisp functions written for Sage (www.sagemath.org), which in turn is written in python. To do so, I tried...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.