January 31st, 2005, 05:05 PM
taking command line input
i'm a novice when it comes to python programming.heres my question,
how can we take command line input in python??
and i was looking for a ready to use function to compare dates. i.e start date and end date and see whether the given dates are valid or not.
January 31st, 2005, 06:42 PM
Command line input in python is accessible via the sys.argv list as strings. Like this:
Note: The above list is empty since Python is running in interactive mode and no arguments were passed to it. Any arguments that are passed to the program will appear here .
>>> import sys
You should look at the strptime() function in the time module, you can use this to covert a time/date string into a comparable form (a Tuple). Pay attention to the flags used for strftime().
Hope this helps,
January 31st, 2005, 06:52 PM
Do you mean the input() and raw_input() functions.
The difference between input() and raw_input() is that input() will interpret python expressions and assign the result of the expression, whereas raw_input() doesn't do anything to the input. So, if you were to enter:
foo = input("Enter a value")
bar = raw_input("Enter another value")
5 * 2 + 3
then foo will be assigned the value 13 and bar will be assigned the string "5 * 2 + 3"
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
February 1st, 2005, 10:59 AM
Thanks guys for your timely help!!
February 1st, 2005, 04:27 PM
i was able to take command line I/P using sys.argv but i did not find the mentioned functions under time module!!
i imported the time module and tried the following stuff,
if i understand this properly i shld be able to use the strptime() function on lt ???
February 1st, 2005, 05:36 PM
Look more closely; it’s there . Anyway to clarify here’s an example that uses the strptime() function to convert two [string] times to a Tuple; this can then be used to compare the two.
At this point it would be handy to understand how this Tuple is laid out:
Each of these values is represented as an integer value, more details can be found in the time modules documentation. You might also be interested in the datetime module, depending on what you plan to do:
Note: the Tuple only contains valid hour (%H) minute (%M) and second (%S) values. This is because they are the only values being passed to strptime().
>>> import time
>>> timeString1 = '22:10:00'
>>> timeString2 = '23:50:00'
>>> timeTuple1 = time.strptime(timeString1, '%H:%M:%S')
>>> timeTuple2 = time.strptime(timeString2, '%H:%M:%S')
(1900, 1, 1, 22, 10, 0, 0, 1, -1)
(1900, 1, 1, 23, 50, 0, 0, 1, -1)
Edit: Please read the stricky thread at the top of this forum regarding how to ask a question; i mentioned this because of the importance Python places on white space.
Last edited by netytan; February 1st, 2005 at 05:41 PM.