我想将本地文件夹同步到Google云端硬盘中的文件夹。我的文件夹结构是
Sync
| -> folders
| -> james
| -> content2
脚本如下所示
FULL_PATH = 'D://Sync\\folders'
DIR_NAME = 'folders'
def folder_upload(service):
parents_id = {}
for root, _, files in os.walk(FULL_PATH, topdown=True):
last_dir = root.split('/')[-1]
pre_last_dir = root.split('/')[-2]
if pre_last_dir not in parents_id.keys():
pre_last_dir = []
else:
pre_last_dir = parents_id[pre_last_dir]
folder_metadata = {'name': last_dir,
'parents': [pre_last_dir],
'mimeType': 'application/vnd.google-apps.folder'}
create_folder = service.files().create(body=folder_metadata,
fields='id').execute()
folder_id = create_folder.get('id', [])
for name in files:
file_metadata = {'name': name, 'parents': [folder_id]}
media = MediaFileUpload(
os.path.join(root, name),
mimetype=mimetypes.MimeTypes().guess_type(name)[0])
service.files().create(body=file_metadata,
media_body=media,
fields='id').execute()
parents_id[last_dir] = folder_id
return parents_id
我在Google驱动器中得到的输出是
答案 0 :(得分:0)
我找到了答案。在root.split('/')行中,分隔符必须为el-rate
而不是'\\'
。
因此,修改后的版本为'\'
。