【问题标题】:Delete files from Colaboratory without moving to Trash从 Colaboratory 删除文件而不移动到废纸篓
【发布时间】:2019-03-28 19:55:11
【问题描述】:

我想立即删除从 Google Colaboratory 笔记本中保存的临时文件,而不会将它们放入回收站。

我在我的脚本中使用 Keras+Tensorflow,并让它在每个训练阶段后保存完整的模型。主要原因是如果脚本因任何原因停止,我可以稍后重新启动它,它会读入最近保存的模型并继续训练。为了节省磁盘空间(它正在使用我的 Google Drive),我让它在每次保存新模型时删除模型的先前版本。我使用标准 python os.remove() 执行此操作,但后来发现我完全填满了我的 Google Drive,因为 os.remove 只是将文件移动到 Trash 文件夹而不是实际删除它们。

我环顾四周,发现对 google colab API 的引用说您必须调用文件对象的 Delete 方法。但是,仅使用文件名来获取对文件对象的引用似乎非常复杂。我假设我没有正确地做到这一点。下面的代码是我想出的解决方法。有一条注释指出我必须用 25 行可读性差的代码替换我的单行代码。

我还应该说,我找到的文档一直表明,我应该能够在基本上一次调用 gdrive.ListFile 中找到该文件,使用类似“name='myfile'”的东西,但每当我尝试这样做时,我都会得到http查询错误。

!pip install -U -q PyDrive
import os
from google.colab import drive
drive.mount('/content/gdrive')
workdir = '/content/gdrive/My Drive/work/2019.03.26.trackingML/eff100_inverted'
os.chdir( workdir )

epoch = 170
fname = 'model_checkpoints/model_epoch%03d.h5' % (epoch)

#--------------------------------------------------------
# Everything below here is to replace the one line:
# os.remove(fname)

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()
gdrive = GoogleDrive(gauth)

# File google colab file object based on path
fullpath = os.path.join(workdir, fname)
mydirs = fullpath.split('/')[3:]
curid = 'root'
for d in mydirs:
    file_list = gdrive.ListFile({'q': "'%s' in parents and trashed=false" % curid}).GetList()
    for file in file_list:
        if file['title'] == d:
          curid = file['id']
          break

if fname.endswith(file['title']):
  print('Found file %s with id %s' % (file['title'], file['id']))
  file.Delete()
else:
  print('Unable to find %s' % fname)

上面的代码几乎可以满足我的要求,但看起来又丑又臃肿。我希望有人可以指出我的 os.remove() 的 1 或 2 行替换,以避免填充我的垃圾箱(和配额)。

【问题讨论】:

    标签: python google-colaboratory pydrive


    【解决方案1】:

    假设您的检查点文件名以“model_epoch”开头

    1) 在 colab 中,将这些语句写在开头的单元格中:

    !pip install -U -q PyDrive
    from pydrive.auth import GoogleAuth
    from pydrive.drive import GoogleDrive
    from google.colab import auth
    from oauth2client.client import GoogleCredentials
    # Authenticate and create the PyDrive client.
    auth.authenticate_user()
    gauth = GoogleAuth()
    gauth.credentials = GoogleCredentials.get_application_default()
    drive = GoogleDrive(gauth)
    

    2) 转到 Drive,右键单击包含检查点文件的文件夹,然后选择 Get shareable link。将复制一个 id。

    3) 在 colab 中,将这个函数写在一个单元格中。 def clearCheckPointFiles():

      file_list = drive.ListFile({'q': "'*******************' in parents and trashed=false"}).GetList()
      for i in range(np.size(file_list)):
        file_name = file_list[i]['title']
        if (file_name[0:11] == 'model_epoch'):
          drive.CreateFile({'id': file_list[i]['id']}).Delete()
    

    4) 将 ***** 替换为步骤 2 中复制链接的 id

    5) 在保存新检查点之前调用clearCheckPointFiles()

    6) 享受吧!

    【讨论】:

    • 这太复杂了……有没有办法不这样做?
    • 很遗憾,我没有找到简单的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    相关资源
    最近更新 更多