To List all the folders in your Google drive (root directory) through Python script














































To List all the folders in your Google drive (root directory) through Python script



Steps :
  1. The all important code :
    1. # To List all folders of G-drive 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 \n\n' % (file['title'], file['id'], file['mimeType']))
  2. In the above program you are importing GoogleDrive class from pydrive.drive library in line 2
  3. In line 4 you are creating an object 'drive' of the GoogleDrive class and are simultaneously passing the 'gauth' object of GoogleAuth class which you had created earlier to give your access to your G drive account.
  4. In line 5 you are calling ListFile method through the drive object and are passing an telling about directory from which you wish to list out the folder ('root' in the above example). Do remember to false the trashed argument if you don't want to list out the folder which have been trashed already.
  5. This ListFile method of GoogleDrive class returns a dictionary with multiple types of keys. In line 6 we are trying to traverse this iterable returned from the ListFile method
  6. In line 7 we are testing each element of this fileList for it's mimeType (which indicates the type of file) as being a folder or not and For all true case we display the file along with it's name('title') and id.

After running the code in a suitable ide you will get the following output :


If you wish to look at the drive for which this is an output instance :


Comments