Reputation: 90
I am trying to create a folder structure on google drive with python. I've checked all documentations, and all similar stackoverflow questions, but it does not work.
The folder structure I want to create is something like
2017
----06
--------13
--------14
I did not manage to create a folder at any other place than root. The layout is beign flat, and I do not see why.
# this method should create a directory tree, from a string like '2017/06/14'
def create_drive_folder(path):
# this method should create a folder with the given parents
# at first it is called with root as parent, for the 2017 folder
# and when the 06 folder should be created it gets [root and the successfully created 2017 folder id]
def create_drive_folder_level(filename, parents):
dirs = drive.ListFile(
{'q': "'{}' in parents and trashed=false and mimeType='application/vnd.google-apps.folder'".format(
parents[-1]['id'])})
try:
# this will give me the parent folder, if it exists
current = [x for x in list(dirs)[0] if x['title'] == filename][0]
except HttpError:
current = None
except IndexError:
current = None
if not current:
meta = {'title': filename, 'parents': [x['id'] for x in [parents[-1]],
'mimeType': 'application/vnd.google-apps.folder'}
current = drive.CreateFile(meta)
current.Upload({'convert': True})
return current
return current
path = path.split('/')
p = [dict(id='root')]
for i in range(len(path)):
p.append(create_drive_folder_level(path[i], p))
create_drive_folder('2017/06/14')
UPDATE:
The Output this code produces:
{'title': '2017', 'parents': ['root'], 'mimeType': 'application/vnd.google-apps.folder'}
GoogleDriveFile({'title': '2017', 'parents': [{'kind': 'drive#parentReference', 'id': '0AC4TRMtjeM-PUk9PVA', 'isRoot': True}], 'mimeType': 'application/vnd.google-apps.folder', 'kind': 'drive#file', 'id': '0By4TRMtjeM-PRjNPT0pXaFpxY2s', ...})
{'title': '06', 'parents': ['0By4TRMtjeM-PRjNPT0pXaFpxY2s'], 'mimeType': 'application/vnd.google-apps.folder'}
GoogleDriveFile({'title': '06', 'parents': [{'kind': 'drive#parentReference', 'id': '0AC4TRMtjeM-PUk9PVA', 'isRoot': True}], 'mimeType': 'application/vnd.google-apps.folder', 'kind': 'drive#file', 'id': '0By4TRMtjeM-PQUs0OWZ0VFlLTmM', ...})
{'title': '14', 'parents': ['0By4TRMtjeM-PQUs0OWZ0VFlLTmM'], 'mimeType': 'application/vnd.google-apps.folder'}
GoogleDriveFile({'title': '14', 'parents': [{'kind': 'drive#parentReference', 'id': '0AC4TRMtjeM-PUk9PVA', 'isRoot': True}], 'mimeType': 'application/vnd.google-apps.folder', 'kind': 'drive#file', 'id': '0By4TRMtjeM-PN2o4MVplZERiUzA', ...})
It seems to me, that I am giving the correct ID-s, when creating the subdirectories. The layout still becomes flat. WHY?
Upvotes: 2
Views: 2394
Reputation: 90
I actually managed to find the solution. The problem was, that the parents must be dicts, not just string ids. So instead of:
{'title': '06', 'parents': ['0By4TRMtjeM-PRjNPT0pXaFpxY2s']}
It should be:
{'title': '06', 'parents': [{'id': '0By4TRMtjeM-PRjNPT0pXaFpxY2s'}]}
Upvotes: 2