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()
tehhowch
  • 9,645
  • 4
  • 24
  • 42
green seek
  • 133
  • 3
  • 11
  • did you define `io`? such as `import io`? – tehhowch Sep 07 '18 at 20:33
  • For example: https://stackoverflow.com/questions/19934248/nameerror-name-datetime-is-not-defined Only the bare minimum is available without importing modules. `io` is one such module that must be imported. – tehhowch Sep 07 '18 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. – tehhowch Sep 07 '18 at 20:42

0 Answers0