Calling a definition

E

elake

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
 
T

Tim Chase

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
 
F

Fredrik Lundh

elake" (if that's supposed to be swedish said:
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>
 
G

Gabriel Genellina

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
 
E

elake

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.
 

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
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top