Solved

Python 2.7 - Passing arguments

Posted on 2016-09-21
8
95 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 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

726 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