python-2.7 - 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 中,点运算符不用于连接字符串,仅用于访问对象的属性和方法。因此,将字符串文字放在点之后,例如.'-i'
,是语法错误。
您可能想做这样的事情,使用该format
方法将{}
占位符替换为提供的值:
camStart = '/..../mjpg_streamer -o {} -i {}'.format(quote(camOutput),quote(camInput))
推荐阅读
- rust - 有没有办法根据常量的存在有条件地编译?
- feathersjs - Featherjs - 添加自定义字段以挂钩上下文对象
- elasticsearch - 如何让 elasticsearch 5.6.3 跨集群搜索工作?
- regex - 仅显示特定的正则表达式组并使用 sed 在 bash 中删除该行的其余部分
- mysql - MySQL对多个分组的最新值求和
- python - 这是使用 smtplib 的方式吗
- python - Python 序数排名
- node.js - IS LIST 在对话流实现中是否与@sys.any 一起使用?
- c++ - 重命名符号时如何排除库包含?
- r - 在 R 中形成重要结果的列表