[Tutor] scratching my head

Clayton Kirkwood crk at godblessthe.us
Mon Aug 3 03:54:51 CEST 2015



> -----Original Message-----
> From: Tutor [mailto:tutor-bounces+crk=godblessthe.us at python.org] On
> Behalf Of Steven D'Aprano
> Sent: Sunday, August 02, 2015 5:49 PM
> To: tutor at python.org
> Subject: Re: [Tutor] scratching my head
> 
> On Sun, Aug 02, 2015 at 02:44:15PM -0700, Clayton Kirkwood wrote:
> 
> > for dir_path, directories, files in os.walk(main_dir):
> >     for file in files:
> > #        print( " file = ", file)
> > #       if( ("(\.jpg|\.png|\.avi|\.mp4)$") not in file.lower() ):
> > #        if(  (".jpg" or ".png" or ".avi" or ".mp4" )  not in
file.lower()
> 
>         name, ext = os.path.splitext(filename)
>         if ext.lower() in ('.jpg', '.png', '.avi', '.mp4'):
>             ...
> 
> 
> > #            del files[file]
> > #
> > #I get an error on int expected here. If I'm able to access by string,
> > why wouldn't I be able to #acess in the del?
> 
> What are you attempting to do here? files is a list of file names:
> 
> files = ['this.jpg', 'that.txt', 'other.pdf'] filename = 'that.txt'
> 
> What do you expect files['that.txt'] to do?
> 
> The problem has nothing to do with del, the problem is that you are trying
to
> access the 'that.txt'-th item of a list, and that is meaningless.


Well, I was expecting that the list entry would be deleted. In other parts
of my code I am using filenames as the index of lists: list[filenames] for
for loops and some ifs where it appears to work. I am able to look at
directories and the files in them by doing this. Check the rest of my
original code. I had one if that complained at the bottom of my code that
complained that the index was supposed to be an in not the list element
value. So I get that the index is supposed to be an int, and I think what is
happening in much of the code is the filename somehow becomes an int and
then the list accesses that way. It's very confusing. Basically, I was using
filenames as indexes into the list.


> 
> 
> >         print( "looking at file  ", file, "  in
> > top_directory_file_list  ", top_directory_file_list )
> 
> What does this print? In particular, what does the last part,
> top_directory_file_list, print? Because the next error:
> 
> >         if file in top_directory_file_list:
> > #error: arg of type int not iterable
> 
> is clear that it is an int.
> 
> > #yet it works for the for loops
> 
> I think you are confusing:
> 
> top_directory_file_list
> 
> directory_file_list


I don't know. If you look at the code that is going thru the directory
filename by filename the prints kick out filename and directories and the
list elements are addressed by "strings", the actual filenames.

What is happening in most of the code looks like what one would expect if
the lists could be indexed by words not ints. As a programmer, I would
expect lists to be addressed via a name or a number. It seems kind of like
dicktionaries. Am I mixing dictionaries and list?


Clayton
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list