January 31st, 2005, 04:05 PM
-
taking command line input
hi guys,
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, 05:42 PM
-
Command line input in python is accessible via the sys.argv list as strings. Like this:
Code:
>>> import sys
>>> sys.argv
['']
>>>
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
.
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().
http://www.python.org/doc/2.4/lib/module-time.html
Hope this helps,
Mark.
January 31st, 2005, 05:52 PM
-
Do you mean the input() and raw_input() functions.
Code:
foo = input("Enter a value")
bar = raw_input("Enter another value")
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:
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, 09:59 AM
-
Thanks guys for your timely help!!
February 1st, 2005, 03:27 PM
-
hi,
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,
import time
lt=time.localtime()
if i understand this properly i shld be able to use the strptime() function on lt ???
any comments
February 1st, 2005, 04: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:
(year, month, dayOfTheMonth, hour, minute, second, dayOfTheWeek, dayOfTheYear, daylightSavingsTime)
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:
http://www.python.org/doc/2.4/lib/module-datetime.html
Code:
>>> import time
>>> timeString1 = '22:10:00'
>>> timeString2 = '23:50:00'
>>>
>>> timeTuple1 = time.strptime(timeString1, '%H:%M:%S')
>>> timeTuple2 = time.strptime(timeString2, '%H:%M:%S')
>>>
>>> timeTuple1
(1900, 1, 1, 22, 10, 0, 0, 1, -1)
>>> timeTuple2
(1900, 1, 1, 23, 50, 0, 0, 1, -1)
>>>
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().
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.
Take care,
Mark.
Last edited by netytan; February 1st, 2005 at 04:41 PM.