By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

Calling a definition

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.