Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python with arguments
#1
here's an example of what i would like to do

    def infos():
        print "His name is John, he is 24"
        print "His name is Brad, he is 17
        print "His name is Marc, he is 41
        print "His name is Leo, he is 51
i would like that when i run my script like this:
" python names.py -j ", it would only print :
Output:
His name is John,he is 24
" python names.py -m " , it would onl print :
Output:
His name is Marc, he is 41
etc..
how would i do that?
thanks
Reply
#2
This will give you a good start.

#!/usr/bin/env python

# reading arguments given on the command line when script invoked
# (if invoked with nothing after script name, will generate a failure)
import sys     # import module that provides command line arguments
x = sys.argv   # assign to x a list of the command line arguments
print(x[1])    # print the 2nd argument (first will be name of involved script)
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 401 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  How do I handle escape character in parameter arguments in Python? JKR 6 1,162 Sep-12-2023, 03:00 AM
Last Post: Apoed2023
Question log.exception() without arguments in old Python versions? cthart 5 1,164 Nov-19-2022, 07:09 PM
Last Post: Gribouillis
  changing the process command line arguments in python Skaperen 3 3,021 Aug-19-2019, 02:40 PM
Last Post: wavic
  python 3.8 and positional arguments vojo 4 2,804 Jun-10-2019, 10:00 PM
Last Post: DeaD_EyE
  Printing arguments in python console. Narendra 1 1,971 Jan-28-2019, 12:00 AM
Last Post: Larz60+
  Functions (Arguments Passing,Changing a mutable ,Assignment to Arguments Names) Adelton 2 3,872 Mar-02-2017, 10:23 PM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020