Google Drive:如何将共享文件夹内容复制到另一个文件夹?

Google Drive:如何将共享文件夹内容复制到另一个文件夹?

我可以访问与我共享的文件夹(假设为 SharedWithMe 文件夹),并且我想备份此文件夹,因为共享将来会结束。那么,是否可以将 SharedWithMe 文件夹内容与 Google Drive 中的另一个文件夹(假设为 MyBackup)同步/复制到另一个文件夹?

答案1

目前,您还不能直接复制共享文件夹。为此,您需要使用 Google Colaboratory:

  1. 在 Colab 上创建新的笔记本
  2. 首先,您需要按如下方式安装驱动器:
from google.colab import drive
drive.mount('/gdrive')

这将要求输入授​​权码。3
. 现在,在单独的选项卡上,打开您的Google 云端硬盘并转到与我分享. 右击并单击 ‘添加快捷方式至驱动器’。此快捷方式是临时的,稍后可以删除。4
. 键入%cd /gdrive/MyDrive/<path-to-the-shortcut>转到该位置,然后键入pwd获取路径。5
. 执行!cp -r 'above-path/.' '/gdrive/My Drive/<destinantion>'

或者你可以避免所有这些,只需执行此操作:

#@title Deeply copy shared folders in Google Drive
from google.colab import drive
import os

print('Mounting Google Drive...')
drive.mount('/gdrive')

src_path = '/gdrive/MyDrive/DE A1' #@param {type: 'string'}
assert os.path.exists(src_path), f"Source '{src_path}' doesn't exist!"

target_path = '/gdrive/MyDrive/Language/German' #@param {type: 'string'}
os.makedirs(target_path, exist_ok=True)
assert os.path.exists(target_path), f"Target '{target_path}' doesn't exist!"

target_path = os.path.join(target_path, os.path.basename(src_path))
print(f'Copying from "{src_path}" to "{target_path}"...')
os.makedirs(target_path, exist_ok=True)
!cp -rf "$src_path"/* "$target_path"  # also work when source is a shortcut

答案2

@Saaransh_Garg 所述方法现在似乎不适用于一些共享的子文件夹。

但是,如果提供了路径,仍然可以复制共享文件,因此,不是依赖本机系统调用来递归复制文件,而是按照脚本(也应该在 colab 笔记本中运行)列出所有文件和文件夹,然后使用以下命令进行复制shutil

from google.colab import drive
import os
import shutil

print('Mounting Google Drive...')
drive.mount('/gdrive')

# List all dirs and sub-dirs
# OS method won't work on links 
def walk_and_copy(root_path, parent_path, target_path, max_depth=3):
    dirs = 0
    copied = 0
    failed = 0
    if max_depth == 0: return (0,0,0)

    print(f"Listing: {root_path}")
    for entry in os.scandir(parent_path):
        subpath = entry.path[len(root_path)+1:]
        target_p = os.path.join(target_path, subpath)

        # DFS
        if entry.is_dir():
           dirs += 1
           os.makedirs(target_p, exist_ok=True)
           d,c,f = walk_and_copy(root_path, entry.path, target_path, max_depth-1)
           dirs += d
           copied += c
           failed += f
        else:
           print(f"copying to {target_p}")
           try:
               shutil.copyfile(entry.path, target_p)
               copied += 1
           except Exception as e:
               print(f"Cannot copy {target_p}, {type(e)}")
               failed += 1
               return (dirs, copied, failed)

src_path = "/gdrive/MyDrive/src_path" #@param {type: 'string'}
assert os.path.exists(src_path), f"Source '{src_path}' doesn't exist!"

target_path = '/gdrive/MyDrive/target_path' #@param {type: 'string'}
os.makedirs(target_path, exist_ok=True) 
assert os.path.exists(target_path), f"Target '{target_path}' doesn't exist!"

target_path = os.path.join(target_path, os.path.basename(src_path))
d, c, f = walk_and_copy(src_path, src_path, target_path, -1)
print(f"dirs listed: {d}, copied files: {c}, failed: {f}")

相关内容