Python Forum
Looping through files, check content and delete - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Looping through files, check content and delete (/thread-10074.html)



Looping through files, check content and delete - metalray - May-11-2018

Dear Python Experts,

I wonder if it is possible to write a small python program
that goes through my 50 000 .json files on my windows machine and deletes every .json file that conains "throttle":"0" or "throttile":"0.0"
as well as an image that is in the same folder and named in the .json file "cam/image_array": "xxxxxxxxx.jpg"

I imagine looping through all .json files and if the pattern is found, extracting first the image name and deleting the image
and second deleting the json file, then go to next file.

Is something like that possible?


RE: Looping through files, check content and delete - buran - May-11-2018

yes. you will need the json module from standard library to read the json files.
there are multiple different ways to iterate over files.