Solved

Python 2.7 - Passing arguments

Posted on 2016-09-21
8
47 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Autoit restart command not working 6 42
Internet Connection Util 8 69
Linux Scripting 3 53
How does this Python sort work? 5 19
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
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.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

19 Experts available now in Live!

Get 1:1 Help Now