How to delete permanently from mounted Drive folder?

岁酱吖の 提交于 2020-06-01 07:45:48

问题


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)

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.

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

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


回答1:


Just have to move the into trash and connect to your drive. From there delete the notebooks permanently.




回答2:


Files will move to bin upon delete, so this neat trick reduces the file size to 0 before deleting (cannot be undone!)


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



回答3:


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.

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)

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

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.

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.


来源:https://stackoverflow.com/questions/57151432/how-to-delete-permanently-from-mounted-drive-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!