为什么我被拒绝重新运行程序?

时间:2018-01-19 06:41:22

标签: python permissions operating-system denied

 import urllib.request
    from bs4 import BeautifulSoup
    import urllib
    import RPi.GPIO as GPIO
    import time
    import os
    import sys
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)

    light_1 = 5

    GPIO.setup(light_1, GPIO.OUT)

    GPIO.output(light_1, 0)

    req = urllib.request.urlopen("http://lapi.transitchicago.com/api/1.0/ttpositions.aspx?key=6b78297846g28427e4359b886742561183677876&rt=red,blue,G,pink,Brn,Org,P,Y")

    content = BeautifulSoup(req, 'xml')

    def Red(xml):
    train_1 = xml.find('rn')
    trainStation = xml.find('nextStaNm')
    trainApp = xml.find('isApp')
    direction = xml.find('destNm')


    if trainApp.text == '1':
    print('Train number '+ train_1.text + ' RED line train to',direction.text,'is now approching ' + trainStation.text)
    time.sleep(1)
    GPIO.output(light_1, True)
    else:
    print('There is no train arriving yet at '+ trainStation.text)
    GPIO.output(light_1, False)
    time.sleep(1)

    def Blue(xml):
    train_2 = xml.select_one('route[name="blue"] train rn').get_text()
    TrainStation = xml.select_one('route[name="blue"] train nextStaNm').get_text()
    TrainApp= xml.select_one('route[name="blue"] train isApp').get_text()
    direction= xml.select_one('route[name="blue"] train destNm').get_text()


    if TrainApp == '1':
    print('Train number '+ train_2 + ' BLUE line train to',direction,'is now 

    approching ' + TrainStation)

    else:
    print('There is no train arriving yet at '+ TrainStation)


    def Green(xml):
    train_3 = xml.select_one('route[name="g"] train rn').get_text()
    TrainStation = xml.select_one('route[name="g"] train nextStaNm').get_text()
    TrainApp= xml.select_one('route[name="g"] train isApp').get_text()
    direction= xml.select_one('route[name="g"] train destNm').get_text()

    if TrainApp == '1':
    print('Train number '+ train_3 + ' GREEN line train to',direction,'is now approching ' + TrainStation)

else:
print('There is no train arriving yet at '+ TrainStation)

def Pink(xml):
train_4 = xml.select_one('route[name="pink"] train rn').get_text()
TrainStation = xml.select_one('route[name="pink"] train nextStaNm').get_text()
TrainApp= xml.select_one('route[name="pink"] train isApp').get_text()
direction= xml.select_one('route[name="pink"] train destNm').get_text()

if TrainApp == '1':
print('Train number '+ train_4 + ' PINK line train to',direction,'is now approching ' + TrainStation)

else:
print('There is no train arriving yet at '+ TrainStation)

    def Brown(xml):
    train_5 = xml.select_one('route[name="brn"] train rn').get_text()
    TrainStation = xml.select_one('route[name="brn"] train 

    nextStaNm').get_text()
        TrainApp= xml.select_one('route[name="brn"] train isApp').get_text()
        direction= xml.select_one('route[name="brn"] train destNm').get_text()

        if TrainApp == '1':
        print('Train number '+ train_5 + ' BROWN line train to',direction,'is now approching ' + TrainStation)

    else:
    print('There is no train arriving yet at '+ TrainStation)


    def Orange(xml):
    train_6 = xml.select_one('route[name="org"] train rn').get_text()
    TrainStation = xml.select_one('route[name="org"] train nextStaNm').get_text()
    TrainApp= xml.select_one('route[name="org"] train isApp').get_text()
    direction= xml.select_one('route[name="org"] train destNm').get_text()

    if TrainApp == '1':
    print('Train number '+ train_6 + ' ORANGE line train to',direction,'is now approching ' + TrainStation)

    else:
    print('There is no train arriving yet at '+ TrainStation)


    def Purple(xml):
    train_7 = xml.select_one('route[name="p"] train rn').get_text()
    TrainStation = xml.select_one('route[name="p"] train nextStaNm').get_text()
    TrainApp= xml.select_one('route[name="p"] train isApp').get_text()
    direction= xml.select_one('route[name="p"] train destNm').get_text()

    if TrainApp == '1':
    print('Train number '+ train_7 + ' PURPLE line train to',direction,'is now approching ' + TrainStation)

    else:
    print('There is no train arriving yet at '+ TrainStation)

    def Yellow(xml):
    train_8 = xml.select_one('route[name="y"] train rn').get_text()
    TrainStation = xml.select_one('route[name="y"] train nextStaNm').get_text()
    TrainApp= xml.select_one('route[name="y"] train isApp').get_text()
    direction= xml.select_one('route[name="y"] train destNm').get_text()

    if TrainApp == '1':
    print('Train number '+ train_8 + ' YELLOW line train to',direction,'is now approching ' + TrainStation)

    else:
    print('There is no train arriving yet at '+ TrainStation)


    if __name__=='__main__':

    Red(content)
    Blue(content)
    Green(content)
    Pink(content)
    Brown(content)
    Orange(content)
    Purple(content)
    ## Yellow(content)
    os.execv(__file__, sys.argv)

我的目标是让这段代码自动运行并继续自我更新,所以我认为这种方式可行(在底部)然而当我运行它时,它给了我[Errno 13] Permisson否认...不确定什么地方出了错。但是,如果任何人有解决方案或其他方式来解决这个问题,我愿意提出其他建议。

我只想让这段代码自行运行并继续更新。好像我每次都要跑步。不是WHILE循环。

1 个答案:

答案 0 :(得分:0)

为了能够执行文件,必须设置执行位(chmod +x your_file.py)。此外,由于它是一个脚本,您需要将脚本的第一行设为shebang

#!/usr/bin/env python3

正如我在评论中所提到的,你真的不需要自己执行。你可能应该做的是这样的事情:

from time import sleep

if __name__ == '__main__':
    while True:
        xml = request_train_status()
        print(get_red_status(xml))
        print(get_blue_status(xml))
        # etc.
        sleep(1) # Don't constantly slam the API with requests

有几分钟,所以我重新编写了你的​​代码。你有很多重复的逻辑。很难推断出发生什么事情以及如何重复你的过程。我提取了GPIO的东西,请求API,并将XML中的状态提取到单独的函数中。我还为线条颜色引入了Enum,为状态引入了namedtuple以使事情更加明显:

from collections import namedtuple
from enum import Enum
import RPi.GPIO as GPIO
from time import sleep
from urllib import request


LIGHT_1 = 5


def setup_gpio():
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)

    GPIO.setup(LIGHT_1, GPIO.OUT)
    GPIO.output(LIGHT_1, 0)


def set_light(on):
    GPIO.output(LIGHT_1, on)


def request_train_status():
    req = request.urlopen('http://lapi.transitchicago.com/api/1.0/' +
                          'ttpositions.aspx?key=' +
                          '5c78297a2f28427e9b87435118367766&' +
                          'rt=red,blue,G,pink,Brn,Org,P,Y')

    return BeautifulSoup(req, 'xml')


class Line(Enum):
    BLUE = 'blue'
    GREEN = 'g'
    PINK = 'pink'
    BROWN = 'brn'
    ORANGE = 'org'
    PURPLE = 'p'
    YELLOW = 'y'


LineStatus = namedtuple('LineStatus', ('approaching', 'num', 'station',
                                       'direction'))


def get_line_status(xml, line):
    color = line.value
    train = xml.select_one(f'route[name="{color}"] train')

    approaching = train.select_one('isApp').get_text() == '1'
    num = train.select_one('rn').get_text()
    station = train.select_one('nextStaNm').get_text()
    direction = train.select_one('destNm').get_text()

    return LineStatus(approaching, num, station, direction)


def print_line_status(status):
    if status.approaching:
        print(f'Train number {status.num} {color.name} line train to ' +
              f'{status.direction} is now approching f{status.station}')
    else:
        print(f'There is no train arriving yet at {status.station}')


def main():
    while True:
        xml = request_train_status()

        for line in Line:
            status = get_line_status(xml, line)
            print_line_status(status)

            if line == Line.RED:
                set_light(status.approaching)

        sleep(5)


if __name__=='__main__':
    main()

显然,我无法对此进行测试,但上述内容肯定有效(每5秒打印一次新数据,给出或采用API响应时间)。