alexa
Facebook
Twitter
LinkedIn
Instagram
Whatsapp
Call Now
Quick Inquiry

using the Sub process module to delete multiple files that have a certain string in python ?

using the Sub process module to delete multiple files that have a certain string in python ?

I would use a slightly different approach to delete these specific files.

First, you will want to make a list of the paths to all files. It should look like this:

 list = ['17-07-39_03-05-2022_Testing.txt', 
        '16-07-34_03-05-2022_Testing.png',
       etc.] 

Then, you want to search this list for paths that include the string 'testing' and put all these paths in a new list:

 testing_list = []
for path in list:
    if path.find('Testing') > 0:
        testing_list.append(path) 

You can now delete all files in the testing_list list. I would use one of the following methods:

  • os.remove() removes a fil
  • os.rmdir() removes an empty directory.
  • shutil.rmtree() deletes a directory and all its contents.

74 0
7

Write a Comments


* Be the first to Make Comment

GoodFirms Badge
GoodFirms Badge

Fix Your Meeting With Our SEO Consultants in India To Grow Your Business Online