idalsin
idalsin

Reputation: 546

OS X Terminal Output in Python

This is going to be the easiest question for someone to solve, but I can't seem to find the answer. I've got a really simple program that prints the date, and time. I'm trying to run it from the terminal in OS X, as that is how it'll be run when I hand it in to my prof. I've been told the way to run this is with a command like this:

XXX-MacBook-Air:~ XXX$ sudo chmod a+x /Users/[path]/currenttime.py

But when I do that, the print statement in the script doesn't output anywhere. I'm not sure if it is supposed to output in terminal, or where it would print to.

Below is my script for anyone who wants more info on my rookie struggles:

import datetime

#!/usr/bin/env python

def currenttime():
date1 = str(datetime.datetime.now())
day = date1[8:10] #not stripped at all
day2= day.lstrip("0") #strip leading zeros
if len(day2) == 2:
    #then we have a two digit day and we should work with the last digit
    if day2[1] == 1:
        day_suffix = 'st'
    elif day2[1] == 2:
        day_suffix = 'nd'
    elif day2[1] == 3:
        day_suffix = 'rd'
    else:
        day_suffix = 'th'
else:
    #one digit day, run off the single digit
    if day2 == 1:
        day_suffix = 'st'
    elif day2 == 2:
        day_suffix = 'nd'
    elif day2 == 3:
        day_suffix = 'rd'
    else:
        day_suffix = 'th'
month = date1[5:7]
#we can use the month to search through a dictionary and return the english name
month_dict= {'01' : 'January', '02': 'February', '03': 'March', '04': 'April', '05': 'May', '06': 'June', '07': 'July', '08': 'August', '09': 'September', '10': 'October', '11': 'November', '12': 'December'}
year = date1[0:4]
hour = date1[11:13]
minute = date1[14:16]
print("Printed on the " + day2 + day_suffix + " day of " + month_dict[month] + ", " + year + " at " + hour + ":" + minute)

currenttime()

Upvotes: 0

Views: 1839

Answers (2)

Robᵩ
Robᵩ

Reputation: 168616

That isn't the way to run it. This is the simple way:

XXX-MacBook-Air:~ XXX$ python /Users/[path]/currenttime.py

In the alternative, you can arrange to avoid the python in the previous line by doing two things:

1) Associate your script with python. Make this line be the very first line of your script: #!/usr/bin/env python

2) Mark this program as executable by running this command once:

XXX-MacBook-Air:~ XXX$ chmod a+x /Users/[path]/currenttime.py

Then, each time you want to run your program do this:

XXX-MacBook-Air:~ XXX$ /Users/[path]/currenttime.py

Upvotes: 4

Hunter McMillen
Hunter McMillen

Reputation: 61512

sudo chmod a+x /Users/[path]/currenttime.py sets the execute permissions on your Python script so that it can be executed. It doesn't actually execute it.

To execute your script call the python interpreter explicitly:

python /Users/[path]/currenttime.py

Or you could/probably should move #!/usr/bin/env python to be the very first line in your file. Then you can execute your python script directly.

/Users/[path]/currenttime.py

Upvotes: 2

Related Questions