我试着在树莓上用我的Raspicam拍照,每张照片都加上一个timestamp。但是我使用的代码不起作用。它给了我一个.."+%H...的语法错误
我已经在代码中混过去了,而且也能做一次,然后强硬的图片文件没有收到任何timestamp上。
有人知道我做错了什么吗?

#!/usr/bin/python

import RPi.GPIO as GPIO, time, os, subprocess, random

gpout = subprocess.check_output("stamp=$(date "+%H%M%S")", stderr=subprocess.STDOUT,shell=True)

gpout = subprocess.check_output("raspistill -t 1 --output /home/pi/photobooth_images/Test${stamp}.jpg", stderr=subprocess.STDOUT, shell=True)

最佳答案

语法错误主要是缺少对日期格式字符串周围的双引号进行转义。这就是导致错误的线路上发生的情况:

gpout = subprocess.check_output("stamp=$(date "+%H%M%S")", stderr=subprocess.STDOUT,shell=True)
                                ^             ^       ^ ^
                    string begins    string ends      string begins and ends after )

您会注意到,代码着色也表示这一点。
在字符串中包含文字双引号有两种方法:
要么转义文字双引号
check_output("stamp=$(date \"+%H%M%S\")" ...

或使用单引号作为字符串分隔符
check_output('stamp=$(date "+%H%M%S")' ...

09-04 03:39
查看更多