使用Google Drive API(Python)创建目录

时间:2013-02-27 16:52:08

标签: google-drive-api

我正在尝试使用Python API在Google云端硬盘中创建一个文件夹。这是我写的函数

def testCreateDir(self):
    try:
        media_body = MediaFileUpload("ALLFS", mimetype='application/vnd.google-apps.folder', resumable=True)
        body = {
                'title': "ALLFS",
                'mimeType': "application/vnd.google-apps.folder"
        }
        root_folder = self._service.files().insert(body=body, media_body=media_body).execute()

        return root_folder['id']
    except errors.HttpError,error:
        print 'An error occured: %s' % error
        return None

这是Trace:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "allfs/service/gdrive.py", line 63, in testCreatedir
    media_body = MediaFileUpload("ALLFS", mimetype='application/vnd.google-apps.folder',      
resumable=True)
  File "/Library/Python/2.7/site-packages/oauth2client/util.py", line 120, in  
positional_wrapper
    return wrapped(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/apiclient/http.py", line 419, in __init__
    fd = open(self._filename, 'rb')
IOError: [Errno 21] Is a directory: 'ALLFS'

其他一切正常:阅读,写作,搜索......有人可以帮忙吗?

非常感谢

1 个答案:

答案 0 :(得分:4)

创建文件夹时无需插入任何媒体体。删除实例化MediaFileUpload的行,不要将media_body传递给插入请求。