对于某人来说,这将是最容易解决的问题,但我似乎找不到答案。我有一个非常简单的程序,可以打印日期和时间。我正在尝试从 OS X 的终端运行它,因为当我将它交给我的教授时,它就是这样运行的。我被告知运行它的方法是使用如下命令:
XXX-MacBook-Air:~ XXX$ sudo chmod a+x /Users/[path]/currenttime.py
但是当我这样做时,脚本中的打印语句不会在任何地方输出。我不确定它是否应该在终端中输出,或者它会打印到哪里。
以下是我的脚本,供任何想了解更多关于我的新秀斗争信息的人使用:
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()