Solved

Python 2.7 - Passing arguments

Posted on 2016-09-21
8
71 Views
Last Modified: 2016-09-21
Hi there,

From the windows shell, I need to start my project by passing an argument to it.

Command line: task.py "These are strings" "9"
Where 9 is a number

print "argument1"
type (argument1)
print("") #CLRF is intended here

print argument2 + 100
type (argument2)

Open in new window


Thanks for your help,
Rene
0
Comment
Question by:ReneGe
  • 5
  • 3
8 Comments
 
LVL 16

Expert Comment

by:gelonida
ID: 41809453
what you are looking foir is sys.argv, which works for windows and other OSes

import sys
args = sys.argv[1:]
if len(args) < 2:
    print("ERROR: at least 2 arguments required, but got only %d"  % len(args))
    sys.exit(1)

argument1, argument2  = args[:2]
argument2 = int(argument2)

print("argument1 %r" % argument1)
print("") #CLRF is intended here

print(int(argument2) + 100)

Open in new window


you might also look at a much more complex but very flexible solution for parsing command line arguments.

the module is called https://docs.python.org/3/library/argparse.html
it is used to parse / validate sys.argv[1:]
0
 
LVL 10

Author Comment

by:ReneGe
ID: 41809470
Hi Gelonida,

Thanks for your prompt response :)

Here is the output I got:
ERROR: at least 2 arguments required, but got only 0

My command line was: task.py "qq" "9"

Cheers
0
 
LVL 16

Accepted Solution

by:
gelonida earned 500 total points
ID: 41809517
hmm that's strange:
just add one line to print the inital sys.argv value

import sys
print("ARGV = %r" % sys.argv)
args = sys.argv[1:]
if len(args) < 2:
    print("ERROR: at least 2 arguments required, but got only %d"  % len(args))
    sys.exit(1)

argument1, argument2  = args[:2]
argument2 = int(argument2)

print("argument1 %r" % argument1)
print("") #CLRF is intended here

print(int(argument2) + 100)

Open in new window


can you open a CMD window, cd to the directory where your script is located and type following commands and post the output that you get for each of line/command below?
task.py "qq" "9"
python task.py "qq" "9"
where python
ftype .py

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 10

Author Comment

by:ReneGe
ID: 41809521
Yea it's working

I omitted to add python in my command line :)

I'll know better next time!

Thanks for your help
Cheers
0
 
LVL 10

Author Comment

by:ReneGe
ID: 41809532
0
 
LVL 16

Expert Comment

by:gelonida
ID: 41809540
Good to know that you got it working.

However on my Windows machine I'm not obliged to type python before the command.

I guess, that your python launcher is not configured as it should be.
0
 
LVL 10

Author Comment

by:ReneGe
ID: 41809561
Maybe I should associate .py extensions to my python.exe
0
 
LVL 10

Author Comment

by:ReneGe
ID: 41809573
Did not work!  Oh well, I guess I can create a new question for this one as well
Thanks :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to count occurrences of each item in an array.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question