newb question: file searching

jaysherby at gmail.com jaysherby at gmail.com
Tue Aug 8 20:18:21 EDT 2006


Something's really not reliable in my logic.  I say this because if I
change the extension to .png then a file in a hidden directory (one the
starts with '.') shows up!  The most frustrating part is that there are
.jpg files in the very same directory that don't show up when it
searches for jpegs.

I tried os.walk('.') and it works, so I'll be using that instead.

jaysherby at gmail.com wrote:
> Here's my code:
>
> def getFileList():
> 	import os
> 	imageList = []
> 	for dirpath, dirnames, filenames in os.walk(os.getcwd()):
> 		for filename in filenames:
> 			for dirname in dirnames:
> 				if not dirname.startswith('.'):
> 					if filename.lower().endswith('.jpg') and not
> filename.startswith('.'):
> 						imageList.append(os.path.join(dirpath, filename))
> 	return imageList
>
> I've adapted it around all the much appreciated suggestions.  However,
> I'm running into two very peculiar logical errors.  First, I'm getting
> repeated entries.  That's no good.  One image, one entry in the list.
> The other is that if I run the script from my Desktop folder, it won't
> find any files, and I make sure to have lots of jpegs in the Desktop
> folder for the test.  Can anyone figure this out?
>
> jaysherby at gmail.com wrote:
> > I'm new at Python and I need a little advice.  Part of the script I'm
> > trying to write needs to be aware of all the files of a certain
> > extension in the script's path and all sub-directories.  Can someone
> > set me on the right path to what modules and calls to use to do that?
> > You'd think that it would be a fairly simple proposition, but I can't
> > find examples anywhere.  Thanks.




More information about the Python-list mailing list