2

我正在使用以下代码检查 Drive 的更改:

    deltaDict = drive_service.changes().list(includeDeleted = True, startChangeId = driveRC.deltaCursor).execute()
    if not str(driveRC.deltaCursor) == str(deltaDict['largestChangeId']):
        print '*** Change Detected ***'
        fileItems = deltaDict['items']
        for item in fileItems:
            isDeleted = item['deleted']
            theFile = item['file']
            fileID = theFile['id']
            fileLabels = theFile['labels']
            fileName = theFile['title'] 
            isTrashed = fileLabels['trashed']

这在一段时间内运行良好。但是,目前,我看到了错误:

    theFile = item['file']
    KeyError: 'file'

但是查看文档在我看来它应该可以工作吗?谁能发现我错过了什么?提前感谢您的帮助。

4

1 回答 1

2

根据文档,item['file']仅当文件未被删除时才存在,因此您只能在item['deleted']为 False 或至少将其包装在 try/except 块中时使用它。

for item in fileItems:
    isDeleted = item['deleted']
    try:
        theFile = item['file']
        # Rest of your code
    except KeyError:
        print "Item deleted"
于 2013-03-14T18:24:31.347 回答