python try语句的语法无效

use*_*604 3 python

在我的脚本中,我有一个大的while:try:loop.在此,我想增加一个图片已成功从我的相机下载和调整的情况下,一些指针,这里是我的代码看起来像我的大python脚本中:

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
Run Code Online (Sandbox Code Playgroud)

picturenumber包含一个字符串'1'开始然后会增加.

我只想要这个运行一个.所以基本上,我通过我的大码连续运行,然后通过大循环每次扫描,我要检查一次该try语句,如果该文件存在,删除一些文件,并增加了指针.

我收到以下错误.

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

非常新的python ...所以希望这不是一个简单的错误:(

Tha*_*Guy 6

你需要一个新的线和一个:.试试这个:

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
Run Code Online (Sandbox Code Playgroud)

finally无论结果如何,您都可以包含一个语句来执行代码:

try:
    #code
except:
    pass
finally:
    #this code will execute whether an exception was thrown or not
Run Code Online (Sandbox Code Playgroud)


Ade*_*taş 5

试试这样,

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
Run Code Online (Sandbox Code Playgroud)

python尝试语法,

try:
   some_code
except:
   pass
Run Code Online (Sandbox Code Playgroud)