Search code examples
pythonfileoperating-systemglob

How to delete same files from a folder that has slightly different names in python?


I have a folder that has wav files. It has filename.wav and filename.WAV.wav for every wav file.

For example an audio file is saved as audio.wav and audio.WAV.wav. I want to remove these repeatingnames. I know i can do it using glob and os.remove. However, how do I select folders only with a specific name?

import os, glob
for filename in glob.glob("thepath/.WAV*"):
os.remove(filename)

I know this wrong as names of the audio files are not being considered. I was thinking somehow to chose 'path/(filenamedoesntmatter)(.WAV) . How do I select this?


Solution

  • This should work:

    import os, glob
    for filename in glob.glob("thepath/*.WAV*"):
        os.remove(filename)
    

    You needed to add an asterisk (*) befor the .WAV*, so that it catches any files that have .WAV in the name, not just any files that have .WAV* at the beginning.