#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    7
    Rep Power
    0

    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.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    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
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    7
    Rep Power
    0
    Thanks guys for your timely help!!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    7
    Rep Power
    0

    Exclamation


    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
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo