简体   繁体   中英

check whether a folder exists with name using python

How to check whether a folder exists in google drive with name using python?

I have tried with the following code:

import requests
import json

access_token = 'token'

url = 'https://www.googleapis.com/drive/v3/files'

headers = {
'Authorization': 'Bearer' + access_token
 }

response = requests.get(url, headers=headers)
print(response.text)
  • You want to know whether a folder is existing in Google Drive using the folder name.
  • You want to achieve it using the access token and requests.get() .

If my understanding is correct, how about this modification? Please think of this as just one of several answers.

Modification points:

  • You can search the folder using the query for filtering the file of drive.files.list.
    • In your case, the query is as follows.
      • name='filename' and mimeType='application/vnd.google-apps.folder'
    • If you don't want to search in the trash box, please add and trashed=false to the query.
  • In order to confirm whether the folder is existing, in this case, it checks the property of files . This property is an array. If the folder is existing, the array has elements.

Modified script:

import requests
import json

foldername = '#####' # Put folder name here.

access_token = 'token'
url = 'https://www.googleapis.com/drive/v3/files'
headers = {'Authorization': 'Bearer ' + access_token}  # Modified
query = {'q': "name='" + foldername + "' and mimeType='application/vnd.google-apps.folder'"}  # Added
response = requests.get(url, headers=headers, params=query)  # Modified
obj = response.json()  # Added
if obj['files']:  # Added
    print('Existing.')  # Folder is existing.
else:
    print('Not existing.')  # Folder is not existing.

References:

If I misunderstood your question, please tell me. I would like to modify it.

You may see this sample code on how to check if destination folder exists and return its ID.

def get_folder_id(drive, parent_folder_id, folder_name):
    """ 
        Check if destination folder exists and return it's ID
    """

    # Auto-iterate through all files in the parent folder.
    file_list = GoogleDriveFileList()
    try:
        file_list = drive.ListFile(
            {'q': "'{0}' in parents and trashed=false".format(parent_folder_id)}
        ).GetList()
    # Exit if the parent folder doesn't exist
    except googleapiclient.errors.HttpError as err:
        # Parse error message
        message = ast.literal_eval(err.content)['error']['message']
        if message == 'File not found: ':
            print(message + folder_name)
            exit(1)
        # Exit with stacktrace in case of other error
        else:
            raise

    # Find the the destination folder in the parent folder's files
    for file1 in file_list:
        if file1['title'] == folder_name:
            print('title: %s, id: %s' % (file1['title'], file1['id']))
            return file1['id']

Also from this tutorial , you can check if folder exists and if not, then create one with the given name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM