0

I am working my way through "Learning Python the Hard Way". I am totally stumped on "Exercise 13: parameters, unpacking, variables". I'm using Sublime Text 3 in a Windows 7 environment.

Here is my code:

from sys import argv
script, first, second = argv
print ("The script is called:", script)
print ("Your first variable:", first)
print ("Your second variable:", second)

Now my questions:

  1. Do I put this in the scripts folder and then reference this from another .py file something like...

    c:\scripts\python\ex11.py one_thing second_thing another_thing
    

    ...or do I use a file in my scripts folder to reference my file in another folder that is holding my .py files?

  2. What is the syntax to point to another file in another folder?

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • 1
    I have no idea why you think other files or folders are required at all. – Daniel Roseman Jun 22 '15 at 22:54
  • "Do I put this in the scripts folder and then reference this from another .py file" No; you *type the command in the terminal window*. It is not Python code; it is a command used by the system to run your script. – Karl Knechtel Aug 09 '22 at 08:20

2 Answers2

1

It helps to determine what the arguments vector, or argv actually does. It's reading in what you pass in to it from the command line.

So, this means that this command should work (provided Python is in your path, and you can just execute the file in this manner):

C:\scripts\python\ex11.py one_thing second_thing another_thing

Note that you'll only see one_thing and second_thing come across; the first value is the name of the script.

Community
  • 1
  • 1
Makoto
  • 104,088
  • 27
  • 192
  • 230
  • To execute the script without typing "python" first, you need to have .PY files associated with Python (e.g. using ASSOC command); if the association is set up correctly, Python actually does not need to be on the path for this. – Mike C Mar 28 '17 at 18:59
0

When using the command line to run programs, arguments can be specified to get the program to run in different ways.

In this example, you will need to open up powershell, run cd c:\scripts\python\, and then python ex11.py one_thing second_thing another_thing

AlecBrooks
  • 524
  • 4
  • 12