gpt4 book ai didi

python - python try 语句上的语法无效

转载 作者:太空宇宙 更新时间:2023-11-03 12:28:30 25 4
gpt4 key购买 nike

在我的脚本中,我有一个大的 While: try: 循环。在此,我想增加一些提示,以防从我的相机成功下载图片并调整大小,这是我的代码在较大的 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

picturenumber包含一个字符串'1'开始,然后会递增。

我只想让它运行一个。所以本质上我是在连续运行我的较大代码,然后每次扫描较大循环时,我想检查一次这个 try 语句,如果文件存在,删除一些文件并增加指针。

我收到以下错误。

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

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

最佳答案

你需要一个新行和一个 :。试试这个:

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

您可以包含一个 finally 语句来执行代码,而不管结果如何:

try:
#code
except:
pass
finally:
#this code will execute whether an exception was thrown or not

关于python - python try 语句上的语法无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15686079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com