这将是一个人最容易解决的问题,但我似乎找不到答案。我有一个非常简单的程序打印日期和时间。我试图在OS中的终端上运行它,因为当我把它交给我的教授时,它就是这样运行的。有人告诉我用这样的命令来运行它:
XXX-MacBook-Air:~ XXX$ sudo chmod a+x /Users/[path]/currenttime.py但是当我这样做的时候,脚本中的print语句不会输出任何地方。我不确定它是否应该在终端输出,或者打印到哪里。
以下是我的脚本,任何人谁想要更多的信息,我的新秀斗争:
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()发布于 2013-09-19 19:23:32
这不是运行它的方法。这是一个简单的方法:
XXX-MacBook-Air:~ XXX$ python /Users/[path]/currenttime.py另外,您可以通过执行以下两项操作来避免前一行中的python:
1)将脚本与python联系起来。让这一行成为脚本的第一行:#!/usr/bin/env python
2)运行此命令一次,将此程序标记为可执行程序:
XXX-MacBook-Air:~ XXX$ chmod a+x /Users/[path]/currenttime.py然后,每次您想要运行您的程序时,请执行以下操作:
XXX-MacBook-Air:~ XXX$ /Users/[path]/currenttime.py发布于 2013-09-19 19:25:17
sudo chmod a+x /Users/[path]/currenttime.py对您的Python设置执行权限,以便可以执行它。它并没有真正执行它。
要执行脚本,请显式调用python解释器:
python /Users/[path]/currenttime.py或者您可以/很可能应该将#!/usr/bin/env python移动到文件中的第一行。然后,您可以直接执行python脚本。
/Users/[path]/currenttime.pyhttps://stackoverflow.com/questions/18903178
复制相似问题