简体   繁体   English

Python中的OS X终端输出

[英]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. 我正在尝试从OS X的终端运行它,因为当我将它交给教授时,它将以这种方式运行。 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. 但是当我这样做时,脚本中的print语句不会输出到任何地方。 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()

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: 或者,您可以通过做两件事来避免上一行中的python

1) Associate your script with python. 1)将脚本与python关联。 Make this line be the very first line of your script: #!/usr/bin/env python 使这一行成为脚本的第一行: #!/usr/bin/env python

2) Mark this program as executable by running this command once: 2)通过运行以下命令将此程序标记为可执行:

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

sudo chmod a+x /Users/[path]/currenttime.py sets the execute permissions on your Python script so that it can be executed. sudo chmod a+x /Users/[path]/currenttime.py设置Python脚本的执行权限,以便可以执行该脚本。 It doesn't actually execute it. 它实际上并不执行它。

To execute your script call the python interpreter explicitly: 要执行脚本,请显式调用python解释器:

python /Users/[path]/currenttime.py

Or you could/probably should move #!/usr/bin/env python to be the very first line in your file. 或者,您可以/大概应该将#!/usr/bin/env python移到文件的第一行。 Then you can execute your python script directly. 然后,您可以直接执行python脚本。

/Users/[path]/currenttime.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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