[英]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.