0

I need to port some R code to Python and want to list all the files present in a directory which is shared with me. In R I can provide a Drive directory's ID to googledrive::drive_ls as per the following:

ID_DIR <- "nprGPfKdhN_LZkyibY9UB43Mzm2D0sCv5"
drive_ls(path = as_id(ID_DIR), type = "spreadsheet")

...assuming we have a directory at the URL https://drive.google.com/drive/u/1/folders/nprGPfKdhN_LZkyibY9UB43Mzm2D0sCv5 the ID is nprGPfKdhN_LZkyibY9UB43Mzm2D0sCv5. (I randomly generated the ID, so this doesn't actually exist.)

How can I list all the files present in a directory so as to later use these file IDs to pull in Google Sheets using Python's gspread?

Is there a more direct way to do this using gspread without listing the files beforehand?

Anil
  • 1,097
  • 7
  • 20
  • 1
    In your situation, are these threads useful? https://stackoverflow.com/q/56857760 https://stackoverflow.com/q/61268195 – Tanaike Mar 10 '22 at 08:56

1 Answers1

0

You can use the Drive API Python Quickstart as a reference.

The quickstart lists all the files in the drive.

The part of the list request is the following:

results = service.files().list(
            pageSize=10, fields="nextPageToken, files(id, name)").execute()
        items = results.get('files', [])

To search the files inside a particular folder you can use the q parameter with the '1234567' in parents query as mentioned in Search for files and folders.

There is also a Python example in there that shows you how to get the search results to file names and IDs of JPEG image files.

Kessy
  • 1,894
  • 1
  • 8
  • 15