Solved

Python 2.7 - Passing arguments

Posted on 2016-09-21
8
66 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

816 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now