Re: [Tutor] any help with this piece of code

2005-05-12 Thread Danny Yoo


On Thu, 12 May 2005, Richard gelling wrote:

> fileToSearchFor = raw_input( "Type in the file name you want to search
> for: ")
>
> if fileToSearchFor in fileList:
> print "%s was found" % fileToSearchFor
> else:
> print "%s was not found" % fileToSearchFor
>
> Could someone explain to me why this pice of code doesn't work, in that
> it works fine up until where I have commented , but when I come to
> search the list for a file name, even though I know the filename is in
> that particular directory, and is present in the list returned by
> 'fileList.append( files )' it always reports that the file is not found.

Hi Richard,


Ah.  TypeError.  *grin* At least, a conceptual one.


The code above accumulates the fileList with the following:

fileList.append( files )


But the problem is that your fileList is a list of lists of file names.
What you really want to do is:

fileList.extend( files )

to hold a flat list of file names in fileList.  The extend() method of a
list allows the list to absorb the elements of the other input list.



Concretely, your fileList probably looks something like this:

[['hello.txt'], ['notes.txt', 'blah.txt']]

where you really want to have something like this instead:

['hello.txt', 'notes.txt', 'blah.txt']


Hope this helps!


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] any help with this piece of code

2005-05-12 Thread Richard gelling
import os

fileList = []
subDirectories = []
directories = []
directoryPath = raw_input( "Type in the path you want to start at: " )

for directory,subDirectory, files in os.walk( directoryPath ):
directories.append( directory )
subDirectories.append( subDirectory )
fileList.append( files )

for Files in fileList:
print Files

# It works fine up until here

fileToSearchFor = raw_input( "Type in the file name you want to search 
for: ")

if fileToSearchFor in fileList:
print "%s was found" % fileToSearchFor
else:
print "%s was not found" % fileToSearchFor

Could someone explain to me why this pice of code doesn't work, in that 
it works fine up until where I have commented , but when I come to 
search the list for a file name, even though I know the filename is in 
that particular directory, and is present in the list returned by 
'fileList.append( files )' it always reports that the file is not found. 
Incidentally if you try this code out, when prompted for the path to 
start at, don't select a large directory as it takes sometime to run. It 
may not be very useful I am just curious as to why the file name cannot 
be found in the list, what am I missing?

Richard Gelling


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor