0

I merged the example code from Downloading Google Documents with the Python quickstart.py. When I try to run the file, I get this error message:

Traceback (most recent call last):
  File "C:\Users\Greenseek\Desktop\drive\quickstart.py", line 45, in <module>
    main()
  File "C:\Users\Greenseek\Desktop\drive\quickstart.py", line 37, in main
    fh = io.BytesIO()
NameError: name 'io' is not defined

I am totally new to this i have pretty no idea why this error has occurred.

The file I'm executing:

from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

# If modifying these scopes, delete the file token.json.
SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'

def main():
    """Shows basic usage of the Drive v3 API.
    Prints the names and ids of the first 10 files the user has access to.
    """
    store = file.Storage('token.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
        creds = tools.run_flow(flow, store)
    service = build('drive', 'v3', http=creds.authorize(Http()))

    # Call the Drive v3 API
    results = service.files().list(
        pageSize=10, fields="nextPageToken, files(id, name)").execute()
    items = results.get('files', [])

    if not items:
        print('No files found.')
    else:
        print('Files:')
        for item in items:
            print('{0} ({1})'.format(item['name'], item['id']))



    file_id = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo'
    request = service.files().export_media(fileId=file_id,
                                             mimeType='application/pdf')
    fh = io.BytesIO()
    downloader = MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print( "Download %d%%." % int(status.progress() * 100))

if __name__ == '__main__':
    main()
3
  • did you define io? such as import io? Commented Sep 7, 2018 at 20:33
  • For example: stackoverflow.com/questions/19934248/… Only the bare minimum is available without importing modules. io is one such module that must be imported. Commented Sep 7, 2018 at 20:39
  • I recommend you work through some Python tutorials to become familiar with its syntax and functionality. An internet search should get you started nicely. Commented Sep 7, 2018 at 20:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.