import io
import os
from googleapiclient import discovery # focuses on connecting to Google APIs
from httplib2 import Http # provides an HTTP client for the app to use
from oauth2client import file, client, tools # helps us manage OAuth2 credentials
import pandas as pd # to make and manage as a dataframe
pd.set_option('display.max_columns', None)
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaIoBaseDownload
SCOPES = 'https://www.googleapis.com/auth/drive.readonly'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
def view_files(folder_id=""):
if folder_id != "":
query = f"parents = '{folder_id}'"
files = DRIVE.files().list(pageSize=1000, q=query).execute().get('files', [])
rows = []
for f in files:
rows.append(f)
df = pd.DataFrame(rows)
print(df.head(10))
print(df["mimeType"].unique())
else:
files = DRIVE.files().list(pageSize=1000, q=None).execute().get('files', [])
rows = []
for f in files:
rows.append(f)
df = pd.DataFrame(rows)
print(df.head(10))
print(df["mimeType"].unique())
if __name__ == "__main__":
view_files(folder_id="13JtdoGqcbsjebfFp")
view_files()
The problem however, is, when I run the default code, the file names have their respective extensions in their filenames. But when I put the folder id, the filenames don’t have their extensions any more.
Why might that be? And how do I get the filenames with their respective extensions?
Attaching the output :
/Users/saatweek/PycharmProjects/replicate_gdrive/venv/bin/python /Users/saatweek/PycharmProjects/replicate_gdrive/trying_on_my_own.py
kind mimeType
0 drive#file application/vnd.google-apps.folder
1 drive#file application/vnd.google-apps.folder
2 drive#file application/vnd.google-apps.document
3 drive#file application/vnd.google-apps.document
4 drive#file application/vnd.google-apps.document
5 drive#file application/vnd.google-apps.document
id name
0 1FKE2_nFjGrbj7oW4HbfkdGYGKb_6jxRz Notes
1 1R1bdiCE7nlqk5aSKXUvadDnJEnhVWWH3 Code
2 1RiWVvd-onvy0eyGjvvkIA4G6hgYtA6bgfXD_fz_Sxd4 Brain Storming Strategy
3 1X4XckEiFW_VpneiVlJRt6aqdvotojWN1Y-k3Xb_N28k behera_satwik_wh3517_hw3
4 18g6xs4NlFubpIiiGimfEbO6hiUSMaPu7DsRNHFYyhao behera_satwik_wh3517_hw2
5 1YffVnlHIJV-VuWZ-iCGVGM6tnFDeymvow7hBlCjQtqE Project Proposal
['application/vnd.google-apps.folder'
'application/vnd.google-apps.document']
kind mimeType
0 drive#file application/vnd.google-apps.document
1 drive#file application/vnd.openxmlformats-officedocument....
2 drive#file application/json
3 drive#file image/jpeg
4 drive#file image/jpeg
5 drive#file image/jpeg
6 drive#file image/jpeg
7 drive#file image/jpeg
8 drive#file image/jpeg
9 drive#file image/jpeg
id name
0 1pXFtguXmxqKhyq2CZyvqFU7zujvcqekiL68RfWqYDyo Testing Notes
1 1RGasEF6Pc7Bx-8Pl3HQupOcd0Z3357BY Data for Gracie Tyler.xlsx
2 1qzi5pcvuuRGKb3z6-MxA7rteijMiAXfJ STAT_653_Lec_2_codes.ipynb
3 1TFKX-5uU4zWPnCjCdcwCLDbH_DEeN15f _MG_9120.jpg
4 1lq2DrP1B4Ihye-YtPVf1xK31m1ggEJLT _MG_9119.jpg
5 1louaxNDzrFV-xqpIN6MgEcscemnj85Ax _MG_9117.jpg
6 15JqtlPwmtWLRRI88PenfS38h2oAd7em- _MG_9118.jpg
7 185I7Gh-DeNpfH3QF3S8A4eBl0DlYSYxU _MG_9116.jpg
8 1tr2tC83e3HqP06Ymfxw92sbd5wU2SqCE _MG_9115.jpg
9 1L_Z5HEF9VTU1ZC3-LiHcFJf7wdF67zGv _MG_9114.jpg
['application/vnd.google-apps.document'
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
'application/json' 'image/jpeg' 'video/quicktime'
'application/vnd.google-apps.spreadsheet'
'application/vnd.google-apps.presentation'
'application/vnd.google-apps.folder'
'application/vnd.google.colaboratory'
'application/vnd.openxmlformats-officedocument.presentationml.presentation'
'image/heif' 'image/webp' 'video/mp4']
Process finished with exit code 0
I want a list of all the files in a particular folder (with all the details like file id, filename (with their extension), and all the folders present within that folder