Python 使用Google Colab删除Google Drive上的文件

Python 使用Google Colab删除Google Drive上的文件,python,google-colaboratory,Python,Google Colaboratory,是否有任何方法可以使用Google Colab删除Google Drive上的文件 我无意中使用Google Colab从主Google Drive文件夹的zip中提取了文件,但找不到删除或移动它们的方法 打开左窗格,找到“文件”选项卡,然后右键单击选择并删除文件。Google Colab在Ubuntu下运行。可以运行以感叹号作为前缀的shell命令。一些例子: # list files in current directory !ls # remove file test.txt in cu

是否有任何方法可以使用Google Colab删除Google Drive上的文件

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


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

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

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

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

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

  • 创建一个新的Colab
  • 使用给定按钮安装驱动器(如果未安装,请尝试多次)
  • 运行给定的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)