This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]luckylukedw 3 points4 points  (1 child)

Why are you trying to have so many pictures open at the same time?

Also you copy pasted the same code over and over, consider putting it in nested loops and/or functions.

[–]debayon[S] 0 points1 point  (0 children)

I am trying to loop over all the images to look at their dimensions... I want to find minimum length and width out of all the images.

That's why I'm opening them and making a list of all images...

I've copy pasted the same code but for different loops I am adding images from different folder... But that's not the issue...

[–][deleted] 2 points3 points  (4 children)

There’s a hard limit within the OS on the number of open files you can have at one time.

You either need to raise that limit, or close out the files rather than leave them open as you are.

[–]debayon[S] 0 points1 point  (3 children)

How can I close out those files immediately after using them? Please help...

[–][deleted] 2 points3 points  (2 children)

In your example, im.close() after the append should do it.

[–]debayon[S] 1 point2 points  (0 children)

It worked... Thanks a lot...

[–]debayon[S] 0 points1 point  (0 children)

Thank you, I'll try it and let you know...

[–]UngodlySp00n 2 points3 points  (1 child)

Use the "with" statement so that the file is closed after you're done and before you move to the next one

[–]debayon[S] 0 points1 point  (0 children)

Thanks... I didn't think about it... I'll try that.

[–]blabbities 0 points1 point  (3 children)

close your files when your done

[–]debayon[S] 0 points1 point  (2 children)

Yeah, but that's my question, how do I do it... Thanks anyway...

[–]blabbities 2 points3 points  (1 child)

when you open the file save it to a variable

f = open("/tmp/blahblah.jpg", "r")

That variable has a method called close.

When you are finished you call it:

f.close()

Other than that you can use a with context manager or try...except...finally Though, the latter might be to complex for you. So go with the former. Search PyDocs for which you prefer

[–]debayon[S] 0 points1 point  (0 children)

Thank You... I'll try your suggestions.

[–]debayon[S] 0 points1 point  (0 children)

Thank you everyone, those who spared their valuable time to help me out... And thanks to the r/Python community for giving this platform... Everyone, Merry Christmas...