在 Mac 终端上运行 Python 脚本

D01001010

我正在尝试在终端中执行 Python 脚本。

在 Python shell 中运行它会做它应该做的事情。它将运行而不会出错,但在终端中执行时没有任何反应。

一旦弄清楚这一点,是否有更有用的方法让程序将“timeAide”和“cancelSleep”字符串输入终端并输入Mac密码。我计划导入 'pyautogui' 来完成所有这些部分,但有没有更好的东西。

#!/usr/bin/env python

#sleepAide: user enters a number to put the computer to sleep
#command for sleep: sudo systemsetup -setcomputersleep 60
#command to cancel sleep: sudo systemsetup -setcomputersleep Never .  

#check python version in terminal: python --version
#shebang line: '#!/usr/bin/python3.6'
#type " 'nano' nameFile.py" in terminal to view code Ex: 'nano namefile.py'

class Sleep(object):
    def __init__(self):
        self.sleepAide()


    def sleepAide(time):                  
        timeAide = 'sudo systemsetup -setcomputersleep '
        cancelSleep = 'sudo systemsetup -setcomputersleep Never'
        time = int(input('In how many minutes would you like to sleep? '))
        if time > 0:
            print(timeAide+' '+str(time))
        elif time == -1:
            print(cancelSleep)
丹尼尔·科林

您只是在声明一个类和方法。您需要实例化__init__要调用函数的类您可以通过在脚本底部的类定义之外添加以下内容来实现:

Sleep()

还有一些其他问题。

  • self.sleepAide()不带time参数地调用,但看起来您不需要它,因为您通过以下方式收集它input
  • 你不及格selfsleepAide定义,但尝试调用它,就好像它是一个实例方法

我在下面做了一些更改以获得一个工作示例:

class Sleep(object):

    def __init__(self):
        self.sleepAide()

    def sleepAide(self):
        timeAide = 'sudo systemsetup -setcomputersleep '
        cancelSleep = 'sudo systemsetup -setcomputersleep Never'
        time = int(input('In how many minutes would you like to sleep? '))
        if time > 0:
            print(timeAide+' '+str(time))
        elif time == -1:
            print(cancelSleep)


Sleep()

使用以下命令运行:

$ python test.py
In how many minutes would you like to sleep? 10
sudo systemsetup -setcomputersleep  10

请记住,该程序实际上并不执行系统命令,它只是打印到控制台。如果您想执行命令,这篇文章可以提供帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在终端上运行Python脚本,然后继续使用终端

如何在Unix控制台或Mac终端上运行Shell脚本?

如何在不使用python3的情况下在终端上运行python脚本?

Python 脚本在使用 VS 代码打开时找不到文件,但在终端上运行正常

1分钟后python脚本停止在终端上运行

从终端运行python脚本

脚本在终端上运行,但不像 cron

如何中断Mac后台运行的python脚本?

设置XAMPP以在Mac上运行python脚本

如何从终端运行python脚本?

无法在超级终端上运行python

从Python中的python终端运行python脚本

运行ruby脚本时终端上Web请求的响应

在终端上打印脚本的结果(在后台运行)

在打开的终端上运行从鹦鹉螺启动的脚本

Shell脚本:命令不是“ foundne”,但在终端上运行良好

在登录和SEE终端上运行我的bash脚本

如何打开终端,在终端上执行python脚本,然后等待脚本结束?

在Mac终端上的doctest中运行+ NORMALIZE WHITESPACE时出错

在终端中执行 python 脚本或在 mac 中执行 pvpython

在python中打开终端并运行python脚本

Python在IDLE中运行,但不在终端上运行?

如何在Mac终端中运行此脚本?

如何从终端在Mac上运行此简单的Perl CGI脚本?

如何在Mac终端中运行Matlab脚本

运行python脚本时如何专注于终端?

在python脚本的终端中运行$ Path命令

从 python 脚本运行终端命令 (Linux)

打开终端时运行 Python 脚本(xterm)