简体   繁体   中英

Python output to command line; Error not defined

I am trying to write a script that takes one argument and writes the output to the command window. For some reason I am getting the error:

NameError: name 'month' not defined

Here is the entire script:

import sys


hex = str(sys.argv)
sys.stdout.write (month(hex) + " " + day(hex) + ", " + year(hex) + " " + hour(hex) + ":" + minute(hex) + ":" + second(hex))

def year (hex):
    year = int(hex[0:2], 16)
    year = year + 1970
    return str(year)

def month (hex):
    month = int(hex[2:4], 16)
    if month == 0:
        month = "January"
        return month
    elif month == 1:
        month = "February"
        return month
    elif month == 2:
        month = "March"
        return month
    elif month == 3:
        month = "April"
        return month
    elif month == 4:
        month = "May"
        return month
    elif month == 5:
        month = "June"
        return month
    elif month == 6:
        month = "July"
        return month
    elif month == 7:
        month = "August"
        return month
    elif month == 8:
        month = "September"
        return month
    elif month == 9:
        month = "October"
        return month
    elif month == 10:
        month = "November"
        return month
    else:
        month = "December"
        return month

def day (hex):
    day = int(hex[4:6], 16)
    return str(day)

def hour (hex):
    hour = int(hex[6:8], 16)
    if hour < 10:
        return "0" + str(hour)
    else:
        return str(hour)

def minute (hex):
    minute = int(hex[8:10], 16)
    if minute < 10:
        return "0" + str(minute)
    else:
        return str(minute)

def second (hex):
    second = int(hex[10:12], 16)
    if minute < 10:
        return "0" + str(second)
    else:
        return str(second)

When I used an online python interpreter to run it, the functions worked fine. I just don't know how to run it from the command line and send the output back to the command window. Thanks

Put the line sys.stdout.write ... after your function definitions.

Please, don't use month for both your function and a variable inside this function.

In python a file is parsed line by line from top to bottom, so the functions month , year , hour , minute and second are not defined yet for this line:

sys.stdout.write (month(hex) + " " + day(hex) + ", " + year(hex) + " " + hour(hex) + ":" + minute(hex) + ":" + second(hex))

Move these function definitions above this line.

And using a local variable with same name as the function name is not a good idea.

As sys.argv returns a list (with first element being the filename), so you can't apply hex over it. Apply hex on the items of the list, ie hex( int(sys.argv[1]) )

>>> lis = ['foo.py', '12']
>>> hex( int(lis[1]) )    #use `int()` as hex expects a number
'0xc'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM