[英]Remove files on Google Drive using Google Colab
有没有办法使用 Google Colab 删除 Google Drive 上的文件?
我不小心使用 Google Colab 从主 Google Drive 文件夹中的 zip 中提取了文件,但找不到删除或移动它们的方法
google colaboratory 中的文件可以按如下方式删除:
import os
os.remove("./filename")
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
我遇到了同样的问题。 我(错误地)将我的数据集(超过 6000 张图像)提取到了谷歌驱动器主文件夹,这导致了许多问题,例如,每次安装驱动器都会比平时花费更长的时间,有时它会出现驱动器超时错误,因为它不能列出所有文件(https://research.google.com/colaboratory/faq.html#drive-timeout )。 要删除所有文件,我尝试使用“os.listdir”但它不起作用(不知道为什么它不起作用)。 这是对我有用的解决方案:
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)
这种方法对我os.listdir
首先获取存储图像的文件的路径,然后很简单 for 循环。
import os
path = "images path"
imgs = os.listdir(path)
for img in imgs:
os.remove(f'{path}/{img}')
从那时起,图像存储目录为空,您可以删除这些文件夹作为 Dwight 的回答。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.