Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Python 2.7 - Passing arguments

Posted on 2016-09-21
8
Medium Priority
?
117 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 17

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 17

Accepted Solution

by:
gelonida earned 2000 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
Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

 
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 17

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

604 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