zeke wonder
zeke wonder

Reputation: 33

Delete the first file in a folder if the length of a folder is more than the limit

I am able to delete the first element of the array of Images but I am not knowing how to delete the actual Image in the path of the folder

CODE BELOW

import cv2

import glob

import numpy as np

images = [19]

files = glob.glob ("./output/*.jpg")

for x in files:

if len(images) >= 19: image_array.pop();

    print("removed first element")

else:

    image = cv2.imread(x)

    image_array.append(images) #append each image to array

    print("no issues")

print('image_array shape:', np.array(images).shape)

cv2.imshow('frame', image_array[0])

cv2.waitKey(0)

Upvotes: 1

Views: 685

Answers (2)

Evinlorth DGreat
Evinlorth DGreat

Reputation: 66

I think this can be helpful: https://thispointer.com/python-how-to-remove-a-file-if-exists-and-handle-errors-os-remove-os-ulink/

import os
# Remove a file
os.remove('/home/somedir/Documents/python/logs')

In your case:

files_list = ["/home/somedir/img1.jpg", "/home/somedir/img2.jpg", ...]
if len(files_list) > 20:
    os.remove(files_list[0])
    del files_list[0]

Upvotes: 1

yrjarv
yrjarv

Reputation: 232

From Grepper: os.remove("demofile.txt")

Upvotes: -1

Related Questions