LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-22-2018, 08:25 PM   #1
plymouth72
LQ Newbie
 
Registered: Feb 2018
Posts: 2

Rep: Reputation: Disabled
python code syntax error


Hello:
I'm new in this forum as with Linux as well. I'm trying to set up an instant reply system using a raspberri pi3 and the pi camera. I already have the main stuff done, but with the code I will need some help from you guys!
This code is already in youtube https://www.youtube.com/watch?v=-QyMxKfBaAE from a guy that use it for the same pourpose that me. My problem is at the moment of run the code in the cmd prompt. there is a syntax error message specifically in the line #42

while not dircreated: if os.path.exists(directory): #add suffix and check again dirsuf = dirsuf + 1 directory = dirpref + "/" + str(dirsuf) + "/" else: #create directory os.makedirs(directory) dircreated = True
camera.start_preview()

here is the complete code in case someone of you can revise it and tell me what is wrong with it.


Code:
import sys
import io
import picamera
import RPi.GPIO as GPIO
import time
import os
from subprocess import Popen
import pygame.display
import pygame.event
#Set up confirguration
isoVal = 0
expmode = 'sports'
postseconds = 1.0
totalseconds = 1.5
thisframerate = 90
playbackframerate = 15
theoplaytime = totalseconds * thisframerate / playbackframerate
firstsleep = theoplaytime + 1
secsleep = firstsleep + 5
#set blank screen behind everything
pygame.display.init()
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
screen.fill((0, 0, 0))
#set up GPIO using BCM Numbering
GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.IN)
camera = picamera.PiCamera()
camera.vflip = True
camera.hflip = True
camera.resolution = (640, 480)
camera.framerate = thisframerate
camera.exposure_mode = 'sports'
camera.iso = isoVal
stream = picamera.PiCameraCircularIO(camera, seconds=7)
camera.start_recording(stream, format='h264', intra_period = 10)
videocount = 1
#set up directory for files
dirpref = time.strftime("%b%d%y")
directory = dirpref + "/"
dircreated = False
dirsuf = 0
while not dircreated: if os.path.exists(directory): #add suffix and check again dirsuf = dirsuf + 1 directory = dirpref + "/" + str(dirsuf) + "/" else: #create directory os.makedirs(directory) dircreated = True
camera.start_preview()
running = True
try: while running: for event in pygame.event.get(): if (event.type == pygame.KEYUP and event.key == pygame.K_c and event.mod & pygame.KMOD_CTRL): pygame.quit() sys.exit() camera.wait_recording(0.2) if GPIO.input(5): camera.wait_recording(postseconds) filemp4 = directory + "race" + str(videocount) + ".mp4" filename = directory + "race" + str(videocount) + ".h264" stream.copy_to(filename, seconds=totalseconds) convertstring = "MP4Box -fps " + str(playbackframerate) + " -add " + filename + " " + filemp4 #playerstring = "omxplayer " + filemp4 os.system(convertstring) #os.system(playerstring) omxc = Popen(['omxplayer', filemp4]) camera.stop_preview() time.sleep(firstsleep) omxc = Popen(['omxplayer', filemp4]) videocount = videocount + 1 time.sleep(secsleep) camera.start_preview()




finally: pygame.display.quit() GPIO.cleanup() camera.stop_recording() camera.stop_preview() camera.close() sys.exit()



Appreciate your help!!

ntonio Caban
removed

Last edited by rtmistler; 02-23-2018 at 06:47 AM. Reason: removed user's personal email
 
Old 02-23-2018, 06:51 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Hi, I've moved your question to the Programming forum to gain it better exposure.

I've also removed your personal email. The intents of the LQ site are that your questions are public and so are the answers so that others may benefit from seeing you work out the solution to your problem, much like the reference link you found to gain this code and start with your efforts.

Finally, I've added [code][/code] tags around your code to place it within a code block. This helps to retain the formatting. Meanwhile the formatting is likely gone when you pasted it inline. If you wish you can edit your original post to past in the code to recover the spacing and formatting.

Sorry I don't have a solution to propose, however my experience with the members from the Programming forum is that they likely will have a great deal of suggestions to help you with your syntax error and the remainder of your problem.

Best Regards, and welcome to LQ.
 
Old 02-23-2018, 07:59 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
it looks like python code and it is not formatted properly.
I don't know if it just the post here or what you tried to execute.

where is this code from (exactly)?
 
Old 02-23-2018, 09:17 AM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Python code is indent sensitive. You have to format it properly for it to work.

jlinkels
 
Old 02-23-2018, 09:23 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
The OP did an inline paste of their code. All indenting was lost. I added the code block around it, however could not restore any formatting because none was in there as it was. We'll have to await their update to see if they can edit or post a new reply with properly formatted code inside code blocks.
 
Old 02-23-2018, 09:30 AM   #6
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
That's a mess, the format anyway.

Code:
>>> import time
>>> dirpref = time.strftime("%b%d%y")
>>> print (dirpref)
Feb2318
>>> directory = dirpref + "/"
>>> print (directory)
Feb2318/
>>> dircreated = False
>>> import os
>>> while not dircreated:
...     if os.path.exists(directory):
...             print ("yes")
...     else:
...             print ("no")
no
Clean that up, so that you can see what you are doing
Code:
while not dircreated:
    if os.path.exists(directory):
        do something
    else:
        do something else
        
#Something like:        
while not dircreated: 
    if os.path.exists(directory): #add suffix and check again 
        dirsuf = dirsuf + 1 
        directory = dirpref + "/" + str(dirsuf) + "/" 
    else: 
        os.makedirs(directory) dircreated = True #create directory
camera.start_preview()
Also you can't put remarks at the front of a line like that.
You'll remark the whole line.

Check your code in python shell as you go.
Code:
>>> dirsuf = 0
>>> dirsuf = dirsuf + 1
>>> print (dirsuf)
1
>>> directory = dirpref + "/" + str(dirsuf) + "/"
>>> print (directory)
Feb2318/1/
 
Old 02-24-2018, 03:55 PM   #7
plymouth72
LQ Newbie
 
Registered: Feb 2018
Posts: 2

Original Poster
Rep: Reputation: Disabled
python syntax error

thanks to every one that spend some time trying to help me.
A i said before, i' m not experinced with linix and sorry for the mess withe code.
I just copy and paste it from the youtube link where I found it.
so formating means to break the lines as you guysmention above?
Can someone re write the whole code as it should be to see if it works?
thanks for the time of all of youguys and ! que viva el hobbie!!!😆🚀
 
Old 02-25-2018, 01:38 AM   #8
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Asking others to rewrite and test code that you have downloaded from some random place on the interwebs is not appropriate here at LQ. Please review the Site FAQ for guidance in posting your questions and general forum usage.

Python code depends on correct indentation as a core part of its "meaning". Multiple examples of code, identical except for the way they are indented, may all run but will produce different results. As no one here can say how the original author intended it to be indented, no one can offer useful help for oyur question.

I would suggest that you return to the place you got the code and ask for help, or at least try to get a working code example.

Good luck!
 
Old 02-25-2018, 01:58 PM   #9
floppy_stuttgart
Senior Member
 
Registered: Nov 2010
Location: EU mainland
Distribution: Debian like
Posts: 1,156
Blog Entries: 5

Rep: Reputation: 107Reputation: 107
If you pay a girl/guy here 300$ per hour, they will work for you. Else, ask for a kindly help for some details. (My recommendation since I spent already few hours in that nice forum.)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Invalid Syntax Error on Python Function antonia_alessandra Linux - Newbie 3 03-31-2013 02:49 AM
having trouble with php code giving a Parse error: syntax error, unexpected '>' ashevillan Programming 6 03-14-2013 10:37 AM
[SOLVED] Python Installed; bash command: python base.py. gets syntax error in base.py cre84j Linux - Software 4 01-23-2011 04:46 PM
[python] syntax Error : invalid syntax Python_user Programming 2 09-06-2009 12:52 PM
python syntax error, what am I doing wrong? Tortanick Programming 1 04-03-2007 10:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration