[Tutor] scratching my head

Steven D'Aprano steve at pearwood.info
Mon Aug 3 02:49:25 CEST 2015


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.


>         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




More information about the Tutor mailing list