[Tutor] How to return a list in an exception object?

Alan Gauld alan.gauld at btinternet.com
Thu Jun 18 13:46:50 CEST 2015


On 18/06/15 11:31, David Aldrich wrote:

> I have a list of paths that contain files I want to check.
>
> paths = [pathA, pathB]
>
> then I check the files in each path and get a list of files that differ in some way:
>
> for path in paths
>      differing_files_list = compare_files(path)
>
> I would like to store the differing_files_list with the associated path

So do just that.
There is a data structure called an associative array or,
in Python terms a dictionary.

If you make the path the key you can associate the list
of bad files with it.

paths = {pathA:[], pathB:[].....}  # initialize with empty lists

for path in paths:
     paths[path] = compare_files(path)

> so that, after doing all comparisons, I can print the all differing files, sorted by path:
>
> for path in paths
>      for file in differing_files_list
>          print(path + file)

becomes:

for path in paths:
     for file in paths[path]:
        print(path+file)  # you might want to use os.path.join() here

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list