Re: newb question: file searching



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@xxxxxxxxx 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@xxxxxxxxx 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.

.



Relevant Pages

  • Re: File extension changes when downloading .tar.Z file
    ... Explorer displays uppercase 8.3 DOS-style filenames ... and turn on the Allow uppercase filenames option. ... They have the file extension '.tar.Z'. ...
    (microsoft.public.windows.inetexplorer.ie6.browser)
  • Re: path stuff
    ... for line in myfile: ... for dirpath, dirnames, filenames in os.walk: ... for dirpath, dirnames, filenames in os.walk: ...
    (comp.lang.python)
  • Re: WSS & WMSDE
    ... Search for filenames and extension". ... > depents on the number of documents your are going to put on the site... ... > You could make file name searching yourself by looping trough all the ...
    (microsoft.public.sharepoint.windowsservices)
  • Re: path stuff
    ... for line in myfile: ... for dirpath, dirnames, filenames in os.walk: ... If you want to omit a directory, and include just filenames matching a pattern: ... for dirpath, dirnames, filenames in os.walk: ...
    (comp.lang.python)
  • Re: Can one tell Explorer to accept filesnames containing only an extension?
    ... that it does not accept filenames that only have an extension. ... I would say it is a poor crossplatform design if You have to rename ... Can't one tell Explorer to just shut up and accept such filenames, ...
    (microsoft.public.windowsxp.customize)