0

This is code for a DSLR timer I'm running on my RaspberryPi. The problem is that whenever I run the file it returns the error:

bash: syntax error near unexpected token `('

I'm assuming that the error must relate to one of the characters following a bracket, but I've searched for around an hour and can't find anything. The lower portion of the script I did from scratch, and as I don't have too much experience with python there may be an error (or errors) there as well. Any help is greatly appreciated.

Some of the code was pulled from this video: 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()
11
  • 2
    Your code is python code. Your error message is bash. You need to run the code under python, not under bash. If the code is in a file called script.py, try running python3 script.py. Commented Oct 8, 2017 at 18:38
  • 1
    Or, including a hashbang. Put #!/usr/bin/env python in the first line. Commented Oct 8, 2017 at 18:39
  • @John1024 Unfortunately that didn't work. Commented Oct 8, 2017 at 19:04
  • @randomir The problem still persists. Commented Oct 8, 2017 at 19:04
  • 2
    @ThatOnePost Baloney. Python and bash are two different programs. Python does not emit bash errors. If you run it as I suggested, you cannot get that bash error. You need to explain exactly what you are doing. Commented Oct 8, 2017 at 19:35

1 Answer 1

3

From the imgur image, the problem is: enter image description here

Instead of:

python3 Camera Controller for Raspberry Pi (Part 4) .py

use:

python3 'Camera Controller for Raspberry Pi (Part 4) .py'

Without the quotes, the shell considers Camera, Controller, for, Raspberry, Pi, (, Part, 4, ), and .py all to be separate. Because ( is shell metacharacter in an illegal position, the shell cannot parse this command line.

With quotes, the whole of the file name is treated as one argument and is passed to python unharmed.

After making this change, there may very well be additional issues with the python code. As kdheepak noted, there may be problems with an import statement. For instance, the code imports from a module sh but my python installation does not include any module by that name.

Sign up to request clarification or add additional context in comments.

2 Comments

sh is a 3rd-party library that tries to offer an idiomatic interface to running arbitrary external programs via liberal use of magic. Inasmuch as it obfuscates what's really going on under the hood, I dislike it intensely -- but while the OP may or may not have it installed (and should really be using subprocess instead), it is a thing that exists.
@CharlesDuffy I do have sh installed, but I'm open to any ideas on how to improve my code as this is my first shot at this program and I'll likely be changing things anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.