javi padilla
javi padilla

Reputation: 453

List of files in a google drive folder with python

I've got the exact same question as the one asked on this post: List files and folders in a google drive folder I don't figure out in the google drive rest api documentation how to get a list of files in a folder of google drive

Upvotes: 25

Views: 62582

Answers (5)

Oscar Monge
Oscar Monge

Reputation: 91

Easiest solution if your are working with google collab.

Connect to your Drive in the collab notebook:

    from google.colab import drive
    drive.mount('/content/drive')

Use the special command '!' with the "ls" command to see the list of files in the path of folder drive you specify.

    !ls PATH OF YOUR DRIVE FOLDER

Example: !ls drive/MyDrive/Folder1/Folder2/

Upvotes: 4

todbott
todbott

Reputation: 631

Here's a hacky-yet-successful solution. This actually gets all the files from a particular Google Drive folder (in this case, a folder called "thumbnails"). I needed to get (not just list) all the files from a particular folder and perform image adjustments on them, so I used this code:

`# First, get the folder ID by querying by mimeType and name
folderId = drive.files().list(q = "mimeType = 'application/vnd.google-apps.folder' and name = 'thumbnails'", pageSize=10, fields="nextPageToken, files(id, name)").execute()
# this gives us a list of all folders with that name
folderIdResult = folderId.get('files', [])
# however, we know there is only 1 folder with that name, so we just get the id of the 1st item in the list
id = folderIdResult[0].get('id')

# Now, using the folder ID gotten above, we get all the files from
# that particular folder
results = drive.files().list(q = "'" + id + "' in parents", pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])

# Now we can loop through each file in that folder, and do whatever (in this case, download them and open them as images in OpenCV)
for f in range(0, len(items)):
    fId = items[f].get('id')
    fileRequest = drive.files().get_media(fileId=fId)
            fh = io.BytesIO()
            downloader = MediaIoBaseDownload(fh, fileRequest)
            done = False
            while done is False:
                status, done = downloader.next_chunk()
    fh.seek(0)
    fhContents = fh.read()
    
    baseImage = cv2.imdecode(np.fromstring(fhContents, dtype=np.uint8), cv2.IMREAD_COLOR)

Upvotes: 28

oussama.benbrahem
oussama.benbrahem

Reputation: 31

# Import PyDrive and associated libraries.
# This only needs to be done once per notebook.
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# Authenticate and create the PyDrive client.
# This only needs to be done once per notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

# List .txt files in the root.
#
# Search query reference:
# https://developers.google.com/drive/v2/web/search-parameters
listed = drive.ListFile({'q': "title contains 'CV'"}).GetList()
for file in listed:
    print('title {}, id {}'.format(file['title'], file['id']))


Upvotes: 3

Luca
Luca

Reputation: 1068

You can look here for an example of how to list files in Drive: https://developers.google.com/drive/api/v3/search-files . You need to construct a query that lists the files in a folder: use

q = "'1234' in parents"

where 1234 is the ID of the folder that you want to list. You can modify the query to list all the files of a particular type (such as all jpeg files in the folder), etc.

Upvotes: 18

J. Blackadar
J. Blackadar

Reputation: 1951

See the API for the available functions...

You can search for files with the Drive API files: list method. You can call Files.list without any parameters, which returns all files on the user's drive. By default, Files.list only returns a subset of properties for a resource. If you want more properties returned, use the fields parameter that specifies which properties to return in the query string q. To make your search query more specific, you can use several operators with each query property.

Upvotes: 2

Related Questions