Reputation: 401
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 :(
Upvotes: 4
Views: 24888
Reputation: 1
if you want to see if the file exist just try this:
check_file = os.path.isfile("data/login_data/data.prx")
if check_file == False:
print("cannot find the file")
if check_file == True:
print("the file already exists")
Upvotes: 0
Reputation: 20739
The syntax for try/except in Python is
try:
# code that might raise the exception
pass
except <exceptions go here>:
# code that should happen when the
# specified exception is/exceptions are
# raised
pass
except <other exceptions go here>:
# different code that should happen when
# the specified exception is/exceptions
# are raised
pass
else:
# code that follows the code that
# might raise the exception, but should
# only execute when the exception isn't
# raised
pass
finally:
# code that will happen whether or not
# an exception was raised
pass
A couple of general guidelines:
except
blocks, place blocks with more specific exceptions higher up (i.e., make sure subclassed exceptions appear before their base). The first block with a matching exception will win.try
. Any code that can raise an exception you are expecting belongs in the try
; any code that should execute only when no exception is raised should go inside the else
. That way if it raises an exception you weren't expecting it doesn't get squashed.Also, you may want to take a look at the subprocess
module instead of using os.system()
.
Upvotes: 1
Reputation: 7233
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
Upvotes: 8
Reputation: 21446
try like this,
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
python try syntax,
try:
some_code
except:
pass
Upvotes: 6