rasyidstat
rasyidstat

Reputation: 615

Remove files on Google Drive using Google Colab

Is there any way to remove files on Google Drive using Google Colab?

I accidentally extracted files from zip on main Google Drive folder using Google Colab but cannot find a way to remove or move them

Upvotes: 3

Views: 13068

Answers (5)

rav2001
rav2001

Reputation: 377

This approach worked for me using os.listdir First get the path of the file where your images are being stored, Then it's simple for loop.

import os
path = "images path"
imgs = os.listdir(path)
for img in imgs:
    os.remove(f'{path}/{img}')

since then the image stored directory is empty you can delete those folders as Dwight's answer.

Upvotes: 1

Rajendra Prajapat
Rajendra Prajapat

Reputation: 326

I encountered the same issue. I extracted(by mistake) my Dataset(more than 6000 images) to the google drive main folder, which led to many issues such as, every time mounting drive will take longer than usual, sometimes it gives drive-timeout error as it can not list all the files(https://research.google.com/colaboratory/faq.html#drive-timeout). To remove all files, I tried with "os.listdir" but it did not work(no idea why it doesn't). Here is the solution which worked for me:

  1. Create a new Colab
  2. Mount drive using given button(if it doesn't mount, try multiple times)
  3. run the given python script(please change the script according to your file format. The files which I wanted to delete start with "frame" and end with "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)

Upvotes: 6

user14305184
user14305184

Reputation:

File in google colaboratory can be removed as follows:

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

Upvotes: 2

Alexandra Dudkina
Alexandra Dudkina

Reputation: 4482

Google Colab runs under Ubuntu. You can run shell commands prefixing them with exclamation mark. Some examples:

# 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

Upvotes: 0

Dwight
Dwight

Reputation: 124

enter image description here

Open left pane, find the files tab, then right click to select and delete a file.

Upvotes: 0

Related Questions