python缩进错误

时间:2013-03-02 04:23:51

标签: python google-docs

python的新手,写了大约一个小时。谷歌docs api和例子很棒。我可以连接和创建文件等。我想将我的所有文件备份到谷歌驱动器,所以尝试使用os.walk并遇到持续缩进错误,我不明白。

#!/usr/bin/python

import httplib2
import pprint
from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.client import Credentials
import os
import sys

rootdir = sys.argv[1]

CLIENT_ID = 'MYCLIENT ID'
CLIENT_SECRET = 'MY SECRET ID'
OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive'

# Redirect URI for installed apps
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'

json_creds = open('backup_credentials.json', 'r').read()
credentials = Credentials.new_from_json(json_creds)
#
# Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
http = credentials.authorize(http)
drive_service = build('drive', 'v2', http=http)

#Here is where the problem starts
for root, subFolders, files in os.walk(rootdir):
    for filename in files:
        filepath = os.path.join(root, filename)
        print (filepath)
        media_body = MediaFileUpload(filepath, mimetype='text/plain', resumable=True)
        body = {'title': filename,'description': 'A test document','mimeType': 'text/plain'}
        file = drive_service.files().insert(body=body, media_body=media_body).execute()
        pprint.pprint(file)

违规错误是行文件= ...

实际错误是

File "./quickstart.py", line 59
  file = drive_service.files().insert(body=body, media_body=media_body).execute()
                                                                              ^
IndentationError: unindent does not match any outer indentation level

1 个答案:

答案 0 :(得分:6)

你有

    body = {
     'title': filename,
     'description': 'A test document',
     'mimeType': 'text/plain'
  }  
  file = drive_service.files().insert(body=body, media_body=media_body).execute()
  pprint.pprint(file)

你什么时候应该:

    body = {
     'title': filename,
     'description': 'A test document',
     'mimeType': 'text/plain'
    }
    file = drive_service.files().insert(body=body, media_body=media_body).execute()
    pprint.pprint(file)

请注意,file =行应缩进到与其缩进的“上方”行相同的级别(body = ...)。