如何从已安装的云端硬盘文件夹中永久删除? [英] How to delete permanently from mounted Drive folder?

查看:151
本文介绍了如何从已安装的云端硬盘文件夹中永久删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个脚本,以便在每次迭代后将我的模型和训练示例上载到Google Drive中,以防发生崩溃或任何导致笔记本无法运行的情况,如下所示:

I wrote a script to upload my models and training examples to Google Drive after every iteration in case of crashes or anything that stops the notebook from running, which looks something like this:

drive_path = 'drive/My Drive/Colab Notebooks/models/'
if path.exists(drive_path):
    shutil.rmtree(drive_path)
shutil.copytree('models', drive_path)

每当我检查自己的Google云端硬盘时,垃圾箱中的几十个已删除模型文件夹都会占用几GB,我必须手动将其删除.

Whenever I check my Google Drive, a few GBs is taken up by dozens of deleted models folder in the Trash, which I have to manually delete them.

google.colab.drive 中唯一的功能似乎就是 mount .

The only function in google.colab.drive seems to be mount and that's it.

根据本教程 shutil.rmtree()会删除目录永久但显然不适用于云端硬盘.

According to this tutorial, shutil.rmtree() removes a directory permanently but apparently it doesn't work for Drive.

推荐答案

可以使用 pydrive 模块.我建议您首先将不需要的文件和文件夹移动到废纸rash"(通常通过在代码中将其删除),然后在任何您认为有必要的时候(例如,要释放一些空间来减轻新DL项目的权重),通过编码以下行来清空垃圾箱.

It is possible to perform this action inside Google Colab by using the pydrive module. I suggest that you first move your unwanted files and folders to Trash (by ordinarily removing them in your code), and then, anytime you think it's necessary (e.g. you want to free up some space for saving weights of a new DL project), empty your trash by coding the following lines.

为了永久清空Google云端硬盘的垃圾箱,请在Google Colab笔记本中编写以下代码行:

In order to permanently empty your Google Drive's Trash, code the following lines in your Google Colab notebook:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
my_drive = GoogleDrive(gauth)

输入验证码并创建有效的GoogleDrive类实例后,输入:

After entering authentication code and creating a valid instance of GoogleDrive class, write:

for a_file in my_drive.ListFile({'q': "trashed = true"}).GetList():
    # print the name of the file being deleted.
    print(f'the file "{a_file['title']}", is about to get deleted permanently.')
    # delete the file permanently.
    a_file.Delete()

如果您不想使用我的建议,并希望永久删除云端硬盘中的特定文件夹,则可能需要进行更复杂的查询并处理 fileId parentId ,并且在对Google Drive API进行查询时,云端硬盘中的文件或文件夹可能有多个父文件夹.

If you don't want to use my suggestion and want to permanently delete a specific folder in your Drive, it is possible that you have to make more complex queries and deal with fileId, parentId, and the fact that a file or folder in your Drive may have multiple parent folders, when making queries to Google Drive API.

有关更多信息:

  • 您可以在此处.
  • 您可以找到 检查文件是否在特定文件夹中的示例 查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆