def create_spreadsheet_with_api(connection, filename):
try:
connection.open(filename)
if (no exception):
raise exception file already exists
if (there exception):
connection.create(filename)
Using pygsheets library which uses google api, I'm trying to create spreadsheet with given name, if it's not already exists.
I receive Exception pygsheets.exceptions.SpreadsheetNotFound:
So I need something like reverse Exception, or if there are better practice of doing it in python your advice will be highly appreciated.
The try
clause has an else
part, which is executed if no exception is raised (similarly named, but totally unrelated to the well known if-else
). So
def create_spreadsheet_with_api(connection, filename):
try:
connection.open(filename)
except FileNotFoundError:
connection.create(filename)
else:
raise FileAlreadyExistsError