我正在编写一个小型 Python 程序,将一些文档加载到 couchdb 中。检查是否已经存在具有特定名称的数据库会非常方便,因此我可以创建一个新的或打开现有的。我想做的是这样的:
import couchdb
def connect(url, dbName):
server = couchdb.Server(url)
if dbName exists: # how do I do this?
return server[dbName]
else:
return server.create(dbName)
我知道 try-except 块可以解决问题,但是没有更优雅的方法吗?