首页 > 解决方案 > SyntaxError:使用管道的无效语法。引用

问题描述

试图在 python 的 raspi3 上创建一个 shell 脚本来启动一个网络摄像头。尝试运行脚本时出现语法错误。

请记住,我是 Python 的新手,但我已经分别尝试了每个以查看打印出来的内容,只有在我组合脚本时才得到这个..

from gpiozero import Button
from pipes import quote
import time
import os

print("your script has started")

camOutput = 'output_http.so -w ./www'
camInput = 'input_raspicam.so -hf'
camStart = '/home/pi/projects/mjpg-streamer/mjpg_streamer -o'.format(quote(camOutput)).'-i'.format(quote(camInput))

print("your script is loaded")

stopButton = Button(26) #shutdown
camButton = Button(25)  #web cam
ledButton = Button(24)  #top led

while True:
        if stopButton.is_pressed:
                time.sleep(1)
        if stopButton.is_pressed:
                os.system("shutdown now -h")
        time.sleep(1)

    camStart = '/home/pi/projects/mjpg-streamer/mjpg_streamer -o'.format(quote(camOutput)).'-i'.format(quote(camInput))
                                                                                              ^
SyntaxError: invalid syntax```

标签: python-2.7raspberry-pi3

解决方案


在 Python 中,点运算符不用于连接字符串,仅用于访问对象的属性和方法。因此,将字符串文字放在点之后,例如.'-i',是语法错误。

您可能想做这样的事情,使用该format方法将{}占位符替换为提供的值:

camStart = '/..../mjpg_streamer -o {} -i {}'.format(quote(camOutput),quote(camInput))

推荐阅读