运行 一个 Python 程序 returns: bash: 意外标记 '(' 附近的语法错误
Running a Python program returns: bash: syntax error near unexpected token '('
这是我在 RaspberryPi 上 运行 设置的 DSLR 定时器的代码。问题是每当我 运行 文件时 returns 错误:
bash: syntax error near unexpected token `('
我假设错误一定与括号后面的字符之一有关,但我已经搜索了大约一个小时,但找不到任何内容。脚本的下半部分是我从头开始做的,因为我对 python 没有太多经验,那里也可能有一个(或多个)错误。非常感谢任何帮助。
部分代码摘自此视频:https://www.youtube.com/watch?v=1eAYxnSU2aw
#Imports various modules.
from time import sleep
from datetime import datetime
from sh import gphoto2 as gp
import signal, os, subprocess
import threading
#######################################################################
#Closes the gphoto2 popup.
def killGphoto2Process():
p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
out, err = p.communicate()
for line in out.splitlines():
if b'gvfsd-gphoto2' in line:
pid = int(line.split(None,1)[0])
os.kill(pid, signal.SIGKILL)
#Creates values for when the pictures were taken.
shot_date = datetime.now().strftime("%Y-%m-%d")
shot_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
#Names the pictures "Sunrises".
picID = "Sunrises"
#Creates commands to take and download pictures then stores them in the
#variables "triggerCommand" and "downloadCommand".
captureCommand = ["--capture-image"]
downloadCommand = ["--get-all-files"]
#Creates a folder to store captured pictures and gives it a location.
folder_name = shot_date + picID
save_location = "/home/pi/Desktop/gphoto/" + folder_name
#Creates or changes where the pictures are saved.
def createSaveFolder():
try:
os.makedirs(save_location)
except:
print("Failed to create the new directory.")
os.chdir(save_location)
#Captures and downloads the pictures.
def captureImages():
gp(captureCommand)
gp(downloadCommand)
#Renames the captured images.
def renameFiles(ID):
for filename in os.listdir("."):
if len(filename) < 13:
if filename.endswith(".JPG"):
os.rename(filename, (shot_time + ID + ".JPG"))
#######################################################################
#Creates a loop that runs every 30 seconds.
def printit():
threading.Timer(30, printit).start()
#Imports the "time" module to get the time.
import time
#Creates variables for hour and minute.
hour = int(time.strftime("%H"))
minutePart1 = int(time.strftime("%M"))
colon = ":"
#Puts a "0" in front of "minute" if "minute" is less than 10.
#This prevents: time(7:9) not equaling sunrise(7:09).
if minutePart1 < 10:
minute = ("0" + str(minutePart1))
else:
minute = (minutePart1)
#Converts the time from 24-Hour to 12-Hour.
if int(hour) > 12:
hour = hour - 12
#Creates variables "time" and "sunrise".
time = (str(hour) + colon + str(minute))
sunrise = "7:09"
#Takes a picture if the time is 7:09 AM.
if time == sunrise :
killGphoto2Process()
createSaveFolder()
captureImages()
#renameFiles(picID)
print("Sunrise!")
print("Currently running \"Camera Controller.py\" ")
printit()
从imgur图片来看,问题是:
而不是:
python3 Camera Controller for Raspberry Pi (Part 4) .py
使用:
python3 'Camera Controller for Raspberry Pi (Part 4) .py'
没有 引号,shell 认为 Camera
、Controller
、for
、Raspberry
, Pi
、(
、Part
、4
、)
和 .py
全部分开。因为 (
是 shell 元字符 在非法位置,所以 shell 无法解析此命令行。
使用 引号,整个文件名被视为一个参数并传递给 python 毫发无损。
进行此更改后,python 代码很可能会出现其他问题。正如 kdheepak 指出的那样,导入语句可能存在问题。例如,代码从模块 sh
导入,但我的 python 安装不包含该名称的任何模块。
这是我在 RaspberryPi 上 运行 设置的 DSLR 定时器的代码。问题是每当我 运行 文件时 returns 错误:
bash: syntax error near unexpected token `('
我假设错误一定与括号后面的字符之一有关,但我已经搜索了大约一个小时,但找不到任何内容。脚本的下半部分是我从头开始做的,因为我对 python 没有太多经验,那里也可能有一个(或多个)错误。非常感谢任何帮助。
部分代码摘自此视频:https://www.youtube.com/watch?v=1eAYxnSU2aw
#Imports various modules.
from time import sleep
from datetime import datetime
from sh import gphoto2 as gp
import signal, os, subprocess
import threading
#######################################################################
#Closes the gphoto2 popup.
def killGphoto2Process():
p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
out, err = p.communicate()
for line in out.splitlines():
if b'gvfsd-gphoto2' in line:
pid = int(line.split(None,1)[0])
os.kill(pid, signal.SIGKILL)
#Creates values for when the pictures were taken.
shot_date = datetime.now().strftime("%Y-%m-%d")
shot_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
#Names the pictures "Sunrises".
picID = "Sunrises"
#Creates commands to take and download pictures then stores them in the
#variables "triggerCommand" and "downloadCommand".
captureCommand = ["--capture-image"]
downloadCommand = ["--get-all-files"]
#Creates a folder to store captured pictures and gives it a location.
folder_name = shot_date + picID
save_location = "/home/pi/Desktop/gphoto/" + folder_name
#Creates or changes where the pictures are saved.
def createSaveFolder():
try:
os.makedirs(save_location)
except:
print("Failed to create the new directory.")
os.chdir(save_location)
#Captures and downloads the pictures.
def captureImages():
gp(captureCommand)
gp(downloadCommand)
#Renames the captured images.
def renameFiles(ID):
for filename in os.listdir("."):
if len(filename) < 13:
if filename.endswith(".JPG"):
os.rename(filename, (shot_time + ID + ".JPG"))
#######################################################################
#Creates a loop that runs every 30 seconds.
def printit():
threading.Timer(30, printit).start()
#Imports the "time" module to get the time.
import time
#Creates variables for hour and minute.
hour = int(time.strftime("%H"))
minutePart1 = int(time.strftime("%M"))
colon = ":"
#Puts a "0" in front of "minute" if "minute" is less than 10.
#This prevents: time(7:9) not equaling sunrise(7:09).
if minutePart1 < 10:
minute = ("0" + str(minutePart1))
else:
minute = (minutePart1)
#Converts the time from 24-Hour to 12-Hour.
if int(hour) > 12:
hour = hour - 12
#Creates variables "time" and "sunrise".
time = (str(hour) + colon + str(minute))
sunrise = "7:09"
#Takes a picture if the time is 7:09 AM.
if time == sunrise :
killGphoto2Process()
createSaveFolder()
captureImages()
#renameFiles(picID)
print("Sunrise!")
print("Currently running \"Camera Controller.py\" ")
printit()
从imgur图片来看,问题是:
而不是:
python3 Camera Controller for Raspberry Pi (Part 4) .py
使用:
python3 'Camera Controller for Raspberry Pi (Part 4) .py'
没有 引号,shell 认为 Camera
、Controller
、for
、Raspberry
, Pi
、(
、Part
、4
、)
和 .py
全部分开。因为 (
是 shell 元字符 在非法位置,所以 shell 无法解析此命令行。
使用 引号,整个文件名被视为一个参数并传递给 python 毫发无损。
进行此更改后,python 代码很可能会出现其他问题。正如 kdheepak 指出的那样,导入语句可能存在问题。例如,代码从模块 sh
导入,但我的 python 安装不包含该名称的任何模块。