简体   繁体   English

如何从安装的 Drive 文件夹中永久删除?

[英]How to delete permanently from mounted Drive folder?

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:我编写了一个脚本,用于在每次迭代后将我的模型和训练示例上传到 Google Drive,以防出现崩溃或任何阻止笔记本运行的情况,如下所示:

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

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 Drive 时,垃圾箱中的数十个已删除模型文件夹会占用几 GB,我必须手动删除它们。

The only function in google.colab.drive seems to be mount and that's it. google.colab.drive唯一的功能似乎是mount ,仅此而已。

According to this tutorial , shutil.rmtree() removes a directory permanently but apparently it doesn't work for Drive.根据本教程shutil.rmtree()永久删除目录,但显然它不适用于 Drive。

It is possible to perform this action inside Google Colab by using the pydrive module.可以使用pydrive模块在 Google Colab 中执行此操作。 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 (eg you want to free up some space for saving weights of a new DL project), empty your trash by coding the following lines.我建议您首先将不需要的文件和文件夹移至废纸篓(通常通过在代码中删除它们),然后在您认为有必要的任何时候(例如,您想释放一些空间以节省新 DL 项目的权重),通过编码以下几行清空垃圾箱。

In order to permanently empty your Google Drive's Trash, code the following lines in your Google Colab notebook:为了永久清空您的 Google Drive 的垃圾箱,请在您的 Google Colab 笔记本中编写以下几行代码:

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)

After entering authentication code and creating a valid instance of GoogleDrive class, write:输入验证码并创建 GoogleDrive 类的有效实例后,编写:

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()

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.如果您不想使用我的建议并希望永久删除 Drive 中的特定文件夹,则可能需要进行更复杂的查询并处理fileIdparentId以及您的文件或文件夹中的文件或文件夹的事实查询 Google Drive API 时,Drive 可能有多个父文件夹。

For more information:想要查询更多的信息:

  • You can find examples of more complex (yet typical) queries, here .您可以在此处找到更复杂(但很典型)的查询示例。
  • You can find an example of Checking if a file is in a specific folder , here .您可以此处找到检查文件是否在特定文件夹中的示例。
  • This statement that Files and folders in Google Drive can each have multiple parent folders may become better and more deeply understood, by reading this post .通过阅读这篇文章,Google Drive中的文件和文件夹可以分别具有多个父文件夹的说法可能会变得更好和更深入地理解。

Files will move to bin upon delete, so this neat trick reduces the file size to 0 before deleting (cannot be undone!)文件将在删除时移动到 bin,所以这个巧妙的技巧在删除之前将文件大小减少到 0(无法撤消!)


import os

delete_filepath = 'drive/My Drive/Colab Notebooks/somefolder/examplefile.png'

open(delete_filename, 'w').close() #overwrite and make the file blank instead - ref: https://stackoverflow.com/a/4914288/3553367
os.remove(delete_filename) #delete the blank file from google drive will move the file to bin instead

Just have to move the into trash and connect to your drive.只需将其移入垃圾箱并连接到您的驱动器即可。 From there delete the notebooks permanently.从那里永久删除笔记本。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM