[Tutor] a few question about my evolving program

Cameron Simpson cs at zip.com.au
Wed Aug 12 05:45:48 CEST 2015


On 11Aug2015 20:23, Clayton Kirkwood <crk at godblessthe.us> wrote:
>Question 1:
>What is the purpose and how is the following definition focused on the *?
>Turns out, you can't actually put the asterisk in the code, so what does it
>mean?
>os.stat(path, *, dir_fd=None, follow_symlinks=True)

It is generally better to make separate posts for separate questions, otherwise 
the discussions get all mixed up. And you can pick better Subject: lines that 
way.

Python function definition syntax and semantics are defined here:

  https://docs.python.org/3/reference/compound_stmts.html#function-definitions

and that in turn points to parameters:

  https://docs.python.org/3/glossary.html#term-parameter

Give them a read. The bare "*" in a function definition says that the following 
keyword parameters (dir_fd and follow_symlinks in your example) may only be 
supplied to function calls in keyword form, i.e.:

  os.stat(pathname, follow_symlinks=True)

Without the bare "*", unused positional parameters are assigned to the keywords 
parameters, allowing:

  os.stat(pathname, None, True)

to set these two. The bare "*" forbids this, which avoids a lot of confusion 
and common errors.

>Question 2:
>My current code:
>See "Look here" below.
[...]
>    for current_filename in current_file_list:
>#        print( "looking at file  ", filename, "  in
>top_directory_file_list:   ", top_directory_file_list )
>#        print( "and in current_directory_path:  ",  current_directory_path)
>
>Look here:
>
>         if current_filename in target_directory_file_list:
>#top_directory_file_list
>That's it:<)) Go down to the bottom now:<))
>
>            current_stat_info = os.stat(current_directory_path + '/' +
>current_filename, follow_symlinks = False )
>            current_file_size = current_stat_info.st_size
>            if current_file_size == target_filename_size[current_filename]:
>                #the filename is a duplicate and the size is a duplicate:
>they are the same file
>                print( "file ", current_filename, "size: ",
>current_file_size, " found in both current_directory_path ",
>current_directory_path,
>                  " and ", target_directory, "\n")
>                duplicate_files =+ 1
>
>             else:
>                print( "file ", current_filename, " not a duplicate\n")
>
>current_filename = 'IMG00060.jpg'
>
>target_directory_file_list = ['2010-11-02 15.58.30.jpg', '2010-11-02
>15.58.45.jpg', '2010-11-25 09.42.59.jpg', '2011-03-19 19.32.09.jpg',
>'2011-05-28 17.13.38.jpg', '2011-05-28 17.26.37.jpg', '2012-02-02
>20.16.46.jpg', '218.JPG', 'honda accident 001.jpg', 'honda accident
>002.jpg', 'honda accident 003.jpg', 'honda accident 004.jpg', 'honda
>accident 005.jpg', 'honda accident 006.jpg', 'honda accident 007.jpg',
>'Image (1).jpg', 'Image.jpg', 'IMG.jpg', 'IMG00003.jpg', 'IMG00040.jpg',
>'IMG00058.jpg', 'IMG_0003.jpg', 'IMG_0004.jpg', 'IMG_0005.jpg',
>'IMG_0007.jpg', 'IMG_0008.jpg', 'IMG_0009.jpg', 'IMG_0010.jpg', 'Mak diploma
>handshake.jpg', 'New Picture.bmp', 'temp 121.jpg', 'temp 122.jpg', 'temp
>220.jpg', 'temp 320.jpg', 'temp 321.jpg', 'temp 322.jpg', 'temp 323.jpg',
>'temp 324.jpg', 'temp 325.jpg', 'temp 326.jpg', 'temp 327.jpg', 'temp
>328.jpg', 'temp 329.jpg', 'temp 330.jpg', 'temp 331.jpg', 'temp 332.jpg',
>'temp 333.jpg', 'temp 334.jpg', 'temp 335.jpg', 'temp 336.jpg', 'temp
>337.jpg', 'temp 338.jpg', 'temp 339.jpg', 'temp 340.jpg', 'temp 341.jpg',
>'temp 342.jpg', 'temp 343.jpg']
>
>As you can see the current_filename does not exist in target_directory_file
>list. Yet, I fall through to the next line. Yes, the indents are all fine: I
>wouldn't have gotten to running code otherwise.  I turned my head upside
>down and still couldn't see why it doesn't work and what I am missing?

Have you put in a print statement to prove this, and also to display 
current_filename and target_directory_file on that next line?

Can you reduce this to a MUCH smaller program (eg 10 lines long) showing the 
same problem? For example by hardwiring the values of current_filename and 
target_directory_file:

  current_filename = 'foo'
  target_directory_file_list = ['2010-11-02 15.58.30.jpg', '2010-11-02
  15.58.45.jpg', '2010-11-25 09.42.59.jpg', '2011-03-19 19.32.09.jpg',
  '2011-05-28 17.13.38.jpg', '2011-05-28 17.26.37.jpg', '2012-02-02
  20.16.46.jpg', '218.JPG', 'honda accident 001.jpg', 'honda accident
  002.jpg', 'honda accident 003.jpg', 'honda accident 004.jpg', 'honda
  accident 005.jpg', 'honda accident 006.jpg', 'honda accident 007.jpg',
  'Image (1).jpg', 'Image.jpg', 'IMG.jpg', 'IMG00003.jpg', 'IMG00040.jpg',
  'IMG00058.jpg', 'IMG_0003.jpg', 'IMG_0004.jpg', 'IMG_0005.jpg',
  'IMG_0007.jpg', 'IMG_0008.jpg', 'IMG_0009.jpg', 'IMG_0010.jpg', 'Mak diploma
  handshake.jpg', 'New Picture.bmp', 'temp 121.jpg', 'temp 122.jpg', 'temp
  220.jpg', 'temp 320.jpg', 'temp 321.jpg', 'temp 322.jpg', 'temp 323.jpg',
  'temp 324.jpg', 'temp 325.jpg', 'temp 326.jpg', 'temp 327.jpg', 'temp
  328.jpg', 'temp 329.jpg', 'temp 330.jpg', 'temp 331.jpg', 'temp 332.jpg',
  'temp 333.jpg', 'temp 334.jpg', 'temp 335.jpg', 'temp 336.jpg', 'temp
  337.jpg', 'temp 338.jpg', 'temp 339.jpg', 'temp 340.jpg', 'temp 341.jpg',
  'temp 342.jpg', 'temp 343.jpg']

  if current_filename in target_directory_file_list:
    print("IN! (unexpected!)")
  else:
    print("NOT IN")

If the small program works correctly, that may point you to the issue in your 
larger program.

Cheers,
Cameron Simpson <cs at zip.com.au>


More information about the Tutor mailing list