Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid syntax on a python try statement

Tags:

python

Within my script I have one large While: try: loop. Within this, I have want to increase some pointers in the case that a picture was successfully downloaded from my camera and resized, here is what my code looks like within my larger python script:

import os.path
try os.path.isfile('/home/pi/CompPictures' + picturenumber + '.JPG'):
    os.system('sudo rm /home/pi/Pictures/IMG_0001.JPG')
    os.system('sudo rm /home/pi/output.bin')
    picturenumber = int(picturenumber))+1
except:
    pass

picturenumber contains a string '1' to start and then will increase.

I'm only wanting this to run one. So essentially I'm running through my larger code continuously, then for every sweep through the larger loop, I want to check this try statement once and if the file exists, delete some files and increase the pointer.

I'm getting the following error.

  File "pijob.py", line 210
    try os.path.isfile('/home/pi/CompPictures' + picturenumber + '.JPG'):
         ^
SyntaxError: invalid syntax

Extremely new to python...so hope it isn't a simple mistake :(

like image 582
user2208604 Avatar asked Dec 09 '22 16:12

user2208604


1 Answers

You need a new line and a :. Try this:

try:
    os.path.isfile('/home/pi/CompPictures' + picturenumber + '.JPG') #
    os.system('sudo rm /home/pi/Pictures/IMG_0001.JPG')
    os.system('sudo rm /home/pi/output.bin')
    picturenumber = int(picturenumber))+1
except:
    pass

You can include a finally statement to execute code regardless of the outcome:

try:
    #code
except:
    pass
finally:
    #this code will execute whether an exception was thrown or not
like image 183
That1Guy Avatar answered Dec 11 '22 07:12

That1Guy