将文件从 Colab 上传到 Google Drive 文件夹

tet*_*edp 5 python-3.x google-colaboratory

我想将文件从 colab 上传到我的谷歌驱动器中的特定文件夹。我可以使用文件夹 ID 和下面的代码片段获取该文件夹:

my_folder = drive.ListFile(
        {'q': "'1QYaM1vaUvdzbdsfWbsolncz1xc2pgnpextuP' in parents"}).GetList()
Run Code Online (Sandbox Code Playgroud)

但我的问题:如何将文件(图像)上传到该文件夹​​?有没有一个函数比如

my_folder.upload(my_file)?

到目前为止,我已经看到了一些 zip 文件的示例,但我不想将其作为 zip 文件上传。

小智 5

**步骤(1) 使用 colab 安装驱动器 **

from google.colab import drive
drive.mount('/content/drive')
Run Code Online (Sandbox Code Playgroud)

步骤(2)导入shutil

import shutil
Run Code Online (Sandbox Code Playgroud)

步骤3 使用shutil复制文件

shutil.copy("file_path","/content/drive/MyDrive/folder_name")
Run Code Online (Sandbox Code Playgroud)


Kor*_*ich 2

我从这个答案中得到

fid = '1QYaM1vaUvdzbdsfWbsolncz1xc2pgnpextuP'
f = drive.CreateFile({"parents": [{"kind": "drive#fileLink", "id": fid}]})
f.SetContentFile( some_path )
f.Upload()
Run Code Online (Sandbox Code Playgroud)