You cannot download a folder directly, the folders within the Drive API are considered as files, with the difference of the MIME type application/vnd.google-apps.folder
As the Drive API documentation says:
A container you can use to organize other types of files on Drive. Folders are files that only contain metadata, and have the MIME type application/vnd.google-apps.folder.
Note: A single file stored on My Drive can be contained in multiple folders. A single file stored on a shared drive can only have one parent folder.
As a workaround, you can list all the files contained within a folder and download them one by one. To build the following example I have based on this:
do.py
def list_and_download():
service = drive_service()
folder_id = FOLDER_ID
# List all files within the folder
results = service.files().list(q="'{}' in parents".format(folder_id), includeItemsFromAllDrives= true, supportsAllDrive=true).execute()
items = results.get("files", [])
print(items)
fh = io.BytesIO()
for item in items:
# download file one by one using MediaIoBaseDownload
if item["mimeType"] != "text/csv":
return
request = service.files().get_media(fileId=item["id"])
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download {}%.".format(int(status.progress() * 100)))
print("Download Complete!")
with open(item["name"], "wb") as f:
f.write(fh.read())
# Do whatever you want with the csv
Documentation
Documentation