'How to read all of the files in a specific directory in google drive using python?
I'm trying to go through all of the files in a folder to read and analyze them in python. There are a large number of these files and I would like to iterate through each file in the folder without having to hardcode the file names in.
drive = GoogleDrive(gauth)
file_list = drive.ListFile({'q': "'1fasSmc7insM4cQOoCik0vmEvYLN6Z5XZ' in parents and trashed=false"}).GetList()
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/oauth2client/clientsecrets.py in _loadfile(filename)
120 try:
--> 121 with open(filename, 'r') as fp:
122 obj = json.load(fp)
FileNotFoundError: [Errno 2] No such file or directory: 'client_secrets.json'
During handling of the above exception, another exception occurred:
InvalidClientSecretsError Traceback (most recent call last)
10 frames
InvalidClientSecretsError: ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)
During handling of the above exception, another exception occurred:
InvalidConfigError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/pydrive/auth.py in LoadClientConfigFile(self, client_config_file)
386 client_type, client_info = clientsecrets.loadfile(client_config_file)
387 except clientsecrets.InvalidClientSecretsError as error:
--> 388 raise InvalidConfigError('Invalid client secrets file %s' % error)
389 if not client_type in (clientsecrets.TYPE_WEB,
390 clientsecrets.TYPE_INSTALLED):
InvalidConfigError: Invalid client secrets file ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)
Solution 1:[1]
To search in the root directory, you can use:
file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
This gives you a list of all files in the root.
To have a list of all files in a sub-directory, you can replace 'root' with the folder id.
folder_id = '****'
file_list = drive.ListFile({'q': "'folder_id' in parents and trashed=false"}).GetList()
Solution 2:[2]
I have a code that I am actively using.
folder_id = '********'
file_list = drive.ListFile({'q': "'{}' in parents and trashed=false".format(folder_id)}).GetList()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ehsan Mohammadi |
Solution 2 | Hakan KAYA |