简体   繁体   English

如何使用 Colaboratory 复制 google 文件?

[英]How to copy a google file with Colaboratory?

I need to copy a folder and its entire content (with subfolders) to another folder in Google Drive.我需要将一个文件夹及其全部内容(带有子文件夹)复制到 Google Drive 中的另一个文件夹。

I tried using Colaboratory like this:我尝试像这样使用 Colaboratory:

from google.colab import drive
drive.mount('/content/drive')

%cd /content/drive/MyDrive
%cp -av FOLDER_TO_COPY NEW_FOLDER_COPY

Folders and files are copied except for google files, which gives me the following error:文件夹和文件被复制,除了谷歌文件,这给了我以下错误:
cp: cannot open 'path_to_file' for reading: Operation not supported
This happends for every.gdoc, .gsheet, .gslides, etc. I cannot convert those files to another format (like.docx or.xlsx) because some have complex formulas that I don't want to screw up.每个.gdoc、.gsheet、.gslides 等都会发生这种情况。我无法将这些文件转换为另一种格式(如 .docx 或 .xlsx),因为有些文件具有我不想搞砸的复杂公式。

I also tried this:我也试过这个:

from shutil import copyfile

copyfile('path_to_file_to_copy', 
         'destination_path')

But got:但得到:
OSError: [Errno 95] Operation not supported: 'path_to_file'

How can I copy Google files using Colaboratory?如何使用 Colaboratory 复制 Google 文件?

Google Documents are not really regular files, you can not copy them into Google Colab. Google 文档并不是真正的常规文件,您不能将它们复制到 Google Colab 中。 If you want to import data from them, you can use a library.如果要从它们导入数据,可以使用库。 For example in order to import Google Sheets data, use gspread例如,为了导入 Google 表格数据,请使用gspread

!pip install --upgrade gspread

And then接着

from google.colab import auth
auth.authenticate_user()

import gspread
from oauth2client.client import GoogleCredentials

gc = gspread.authorize(GoogleCredentials.get_application_default())

worksheet = gc.open('Your spreadsheet name').sheet1

# get_all_values gives a list of rows.
rows = worksheet.get_all_values()
print(rows)

# Convert to a DataFrame and render.
import pandas as pd
pd.DataFrame.from_records(rows)

If you want to copy other files, remove the Google Documents from the folder and use the shell command如果要复制其他文件,请从文件夹中删除 Google 文档并使用 shell 命令

!cp -r SRC DEST

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 csv 文件(并使用它)从谷歌驱动器上传到谷歌合作实验室 - How to upload csv file (and use it) from google drive into google colaboratory 在Google colaboratory中打开上传的文件 - Opening uploaded file in google colaboratory 如何访问Google合作实验室根目录中的csv文件? - How to access csv file in the root folder of the Google Colaboratory? 如何在Google Colaboratory中导入和读取shelve或Numpy文件? - How to import and read a shelve or Numpy file in Google Colaboratory? 如何在 Google colaboratory 上使用 GloVe 词嵌入文件 - How to use GloVe word-embeddings file on Google colaboratory 如何使用命令行 arguments 在 google-colaboratory 中运行 python 文件? - How to run python file in google-colaboratory with comand line arguments? 如何在 Colaboratory Google 上使用 Selenium? - How to use Selenium on Colaboratory Google? 如何从 Google Colaboratory 将 dataframe 复制到我的剪贴板(或解决方法)? - How can I copy a dataframe to my clipboard (or workaround) from Google Colaboratory? 无论如何我可以在Google colaboratory下载该文件吗? - Is there anyway I can download the file in Google colaboratory? 如何在 google colaboratory 上使用 GPU 升级 tensorflow - How to upgrade tensorflow with GPU on google colaboratory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM