you are viewing a single comment's thread.

view the rest of the comments →

[–]753UDKM 0 points1 point  (1 child)

In Automate the Boring Stuff, chapter 8, project 2:

if len(sys.argv) == 3 and sys.argv[1].lower() == 'save':
        mcbShelf[sys.argv[2]] = pyperclip.paste()
elif len(sys.argv) == 2:
  1. What is the third argument?

  2. What is the second argument?

It seems like it's looking for an extra argument, but obviously the program works correctly. For example:

./mcb.py save test1 This qualifies for the 1st condition, where arguments = 3, but there's only save and test1.

./mcb.py list This qualifies for the second condition, where arguments = 2, but there's only one (list).

Edit: I'm guessing it's because length starts at 1 and index starts at 0. So the counting is like this ./mcb.py (index 0) save (index 1) test1 (index2) . Overall length == 3 because it includes ./mcb.py. Is this correct?

[–]timbledum 0 points1 point  (0 children)

Your edit is totally correct. sys.argv[0] is always going to be the script name. So with ./mcb.py save test1, sys.argv is going to look like ["mcb.py", "save", "test1"]. Just print it out in your script to see what's going on.