使用Google Colab删除Google云端硬盘上的文件

时间:2020-06-21 08:39:50

标签: python google-colaboratory

是否可以使用Google Colab在Google云端硬盘上删除文件?

我意外地使用Google Colab从Google Drive主文件夹的zip中提取了文件,但是找不到删除或移动它们的方法

4 个答案:

答案 0 :(得分:1)

可以按以下方式删除google colaboratory中的文件:

import os
os.remove("./filename")

答案 1 :(得分:0)

enter image description here

打开左窗格,找到“文件”选项卡,然后右键单击以选择和删除文件。

答案 2 :(得分:0)

Google Colab在Ubuntu下运行。您可以运行带有惊叹号的shell命令。一些例子:

# list files in current directory
!ls

# remove file test.txt in current directory
!rm ./test.txt

# remove all txt files in current directory
!rm ./*txt

# remove directory "sample_data" (with files and subdirectories) in current directory
!rm -rf ./sample_data

答案 3 :(得分:0)

我遇到了同样的问题。我(错误地)将我的数据集(超过 6000 张图像)提取到了谷歌驱动器主文件夹,这导致了许多问题,例如,每次安装驱动器都需要比平时更长的时间,有时会出现驱动器超时错误,因为它不能列出所有文件(https://research.google.com/colaboratory/faq.html#drive-timeout)。要删除所有文件,我尝试使用“os.listdir”但它不起作用(不知道为什么它不起作用)。这是对我有用的解决方案:

  1. 创建一个新的 Colab
  2. 使用给定的按钮挂载驱动器(如果没有挂载,请尝试多次)
  3. 运行给定的python脚本(请根据您的文件格式更改脚本。我要删除的文件以“frame”开头,以“jpg”结尾。
    import os
    import glob
    # my all files starts with "frame" and ends with ".jpg"
    fileList = glob.glob('/content/drive/MyDrive/frame*.jpg')
    print("Number of files: ",len(fileList))
    
    for filePath in fileList:
        try:
            os.remove(filePath)
        except:
            print("Error while deleting file : ", filePath)