tet*_*edp 12 python-3.x tensorflow google-colaboratory
目前,我可以使用以下命令将文件下载为单个文件
files.download(file_name)
Run Code Online (Sandbox Code Playgroud)
我还尝试使用下面的代码片段将它们上传到驱动器,但是它将它们作为单个文件上传。
uploaded = drive.CreateFile({'title': file_name})
uploaded.SetContentString('Sample upload file content')
uploaded.Upload()
print('Uploaded file with ID {}'.format(uploaded.get('id')))
Run Code Online (Sandbox Code Playgroud)
如何将多个文件作为文件夹下载到本地计算机?或者如何将这些文件作为文件夹上传到我的Google驱动器?
小智 22
我创建了一个zip文件:
!zip -r /content/file.zip /content/Folder_To_Zip
Run Code Online (Sandbox Code Playgroud)
比起我已经下载了该zip文件:
from google.colab import files
files.download("/content/file.zip")
Run Code Online (Sandbox Code Playgroud)
Ara*_*d R 11
!zip -r /content/sample_data.zip /content/sample_data
# change sample_data.zip to your desired download name Ex: nothing.zip
# change sample_data to your desired download folder name Ex: ner_data
Run Code Online (Sandbox Code Playgroud)
I found that:
!zip -r ./myresultingzippedfolderwithallthefiles.zip ./myoriginalfolderwithallthefiles/
Run Code Online (Sandbox Code Playgroud)
worked for me in colab.
这里.
可以是您的主目录或您的原始目录myoriginalfolderwithallthefiles
以及myresultingzippedfolderwithallthefiles.zip
将在何处创建的目录。根据需要更改目录。
小智 8
就我而言,我必须下载包含笔记本构建的每个模型的 h5 文件(用于提交大学项目)的整个文件夹。我发现下载该文件夹以及该文件夹中的所有文件的最简单方法是将文件夹拖放到同一文件夹树中的“我的驱动器”文件夹中。
显然我后来从 Google Drive 下载了该文件夹。
您可以使用代码来压缩文件夹并使用files
.
#@title Utility to zip and download a directory
#@markdown Use this method to zip and download a directory. For ex. a TB logs
#@markdown directory or a checkpoint(s) directory.
from google.colab import files
import os
dir_to_zip = 'dir_name' #@param {type: "string"}
output_filename = 'file.zip' #@param {type: "string"}
delete_dir_after_download = "No" #@param ['Yes', 'No']
os.system( "zip -r {} {}".format( output_filename , dir_to_zip ) )
if delete_dir_after_download == "Yes":
os.system( "rm -r {}".format( dir_to_zip ) )
files.download( output_filename )
Run Code Online (Sandbox Code Playgroud)
小智 4
将此代码复制到单元格中,然后更改 2 个字段文件名和folders_or_files_to_save。它将所有文件夹或文件压缩到一个 zip 文件中,并将其保存在您的 Google 云端硬盘中
#@title save yo data to drive
filename = "kerasmodel" #@param {type:"string"}
folders_or_files_to_save = "keras_model.h5" #@param {type:"string"}
from google.colab import files
from google.colab import auth
from googleapiclient.http import MediaFileUpload
from googleapiclient.discovery import build
def save_file_to_drive(name, path):
file_metadata = {
'name': name,
'mimeType': 'application/octet-stream'
}
media = MediaFileUpload(path,
mimetype='application/octet-stream',
resumable=True)
created = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
print('File ID: {}'.format(created.get('id')))
return created
extension_zip = ".zip"
zip_file = filename + extension_zip
# !rm -rf $zip_file
!zip -r $zip_file {folders_or_files_to_save} # FOLDERS TO SAVE INTO ZIP FILE
auth.authenticate_user()
drive_service = build('drive', 'v3')
destination_name = zip_file
path_to_file = zip_file
save_file_to_drive(destination_name, path_to_file)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11401 次 |
最近记录: |