To list out all the files in a folder with given ID through python script using Pydrive Module














































To list out all the files in a folder with given ID through python script using Pydrive Module



Steps :
  1. The all important code :
    1. # To List all folders of G-drive (root directory) with thier names, unique ID and MimeType
    2. from pydrive.drive import GoogleDrive

    3. drive = GoogleDrive(gauth)
    4. fileList = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
    5. for file in fileList:
    6.     if(file['mimeType'] == "application/vnd.google-apps.folder"):
    7.         print('Title: %s, ID: %s AND mimeType: %s ' % (file['title'], file['id'], file['mimeType']))
    8.         
    9. # Display all files in a folder with a given ID.
    10. folder_id = input("Enter the ID folder you wish to open : ")
    11. folder_id = "'" + folder_id + "'"
    12. file_list = drive.ListFile({'q': folder_id + " in parents and trashed=false"}).GetList()
    13. for file1 in file_list:
    14.   print('title: %s, id: %s' % (file1['title'], file1['id']))
  2. First of all through line 1 to 8 we are trying to list out all the folders in the root directory of Google drive to provide a list of folders with their ID to choose from for displaying the content inside the folder.
  3. In line 11 and 12 we taking an input from the user for his choice of file folder and storing his chosen folder ID in a string variable named 'folder_id'.
  4. Now in line 13 using the drive object of GoogleDrive class we are calling the ListFile Method to get all the files present inside the given directory of Google drive.
  5. Finally in line 14 and 15 we are looping the list provided by the ListFile method to display all the content inside the given directory with their 'title'  and 'ID'.



Following is the output instance obtained after running above code in a suitable ide :


Comments