162

I want to delete all files with the extension .bak in a directory. How can I do that in Python?

Sled
  • 17,743
  • 27
  • 115
  • 155
slh2080
  • 1,631
  • 2
  • 11
  • 4
  • 5
    @slh2080: Posting "Solved" isn't what you do on this site. What you do is pick the answer that you actually used and click the check-mark to indicate that it solved your problem. – S.Lott Jan 03 '10 at 17:05
  • 1
    Sorry. Thanks for pointing out my mistake. It's not homework, just learning python in my spare time. I clicked on the answer I used but that was before I saw ghostdog74 response. – slh2080 Jan 10 '10 at 04:27
  • 2
    note: to delete an entire directory tree [`shutil.rmtree(path)` could used](https://docs.python.org/3/library/shutil.html#shutil.rmtree). – jfs Jul 02 '15 at 13:37
  • Possible duplicate of [How to delete the contents of a folder in Python?](https://stackoverflow.com/questions/185936/how-to-delete-the-contents-of-a-folder-in-python) – FabioSpaghetti Feb 28 '19 at 10:38

7 Answers7

306

Via os.listdir and os.remove:

import os

filelist = [ f for f in os.listdir(mydir) if f.endswith(".bak") ]
for f in filelist:
    os.remove(os.path.join(mydir, f))

Using only a single loop:

for f in os.listdir(mydir):
    if not f.endswith(".bak"):
        continue
    os.remove(os.path.join(mydir, f))

Or via glob.glob:

import glob, os, os.path

filelist = glob.glob(os.path.join(mydir, "*.bak"))
for f in filelist:
    os.remove(f)

Be sure to be in the correct directory, eventually using os.chdir.

miku
  • 172,072
  • 46
  • 300
  • 307
  • 21
    Your first example is using redundant for loops. You can one pass with - [ os.remove(f) for f in os.listdir(".") if f.endswith(".bak") ] - as list comprehensions are meant to be used. Or you can move the 'if' in the comprehension into the for loop - for f in os.listdir("."): if f.endswith(".bak"): os.remove(f) – dragonjujo Jan 03 '10 at 16:47
  • @slh2080 Since you say the problem has been solved, why not mark the answer as the correct answer? – blwy10 Jan 03 '10 at 17:00
  • 5
    Watch out the os.listdir(".")!!! I used this code and forgot to change the path, all my code were gone!!! Tried two different utilities to recover but with no luck!! – Lei Guo Nov 29 '16 at 03:51
  • @LeiGuo Fixed that. – yugr Oct 08 '17 at 06:49
  • @dragonjujo I would argue that invoking side effects are very much _not_ how list comprehensions are meant to be used. – marcelm Oct 27 '20 at 10:41
26

Use os.chdir to change directory . Use glob.glob to generate a list of file names which end it '.bak'. The elements of the list are just strings.

Then you could use os.unlink to remove the files. (PS. os.unlink and os.remove are synonyms for the same function.)

#!/usr/bin/env python
import glob
import os
directory='/path/to/dir'
os.chdir(directory)
files=glob.glob('*.bak')
for filename in files:
    os.unlink(filename)
unutbu
  • 777,569
  • 165
  • 1,697
  • 1,613
24

In Python 3.5, os.scandir is better if you need to check for file attributes or type - see os.DirEntry for properties of the object that's returned by the function.

import os 

for file in os.scandir(path):
    if file.name.endswith(".bak"):
        os.unlink(file.path)

This also doesn't require changing directories since each DirEntry already includes the full path to the file.

Yi Jiang
  • 48,053
  • 16
  • 135
  • 134
8

you can create a function. Add maxdepth as you like for traversing subdirectories.

def findNremove(path,pattern,maxdepth=1):
    cpath=path.count(os.sep)
    for r,d,f in os.walk(path):
        if r.count(os.sep) - cpath <maxdepth:
            for files in f:
                if files.endswith(pattern):
                    try:
                        print "Removing %s" % (os.path.join(r,files))
                        #os.remove(os.path.join(r,files))
                    except Exception,e:
                        print e
                    else:
                        print "%s removed" % (os.path.join(r,files))

path=os.path.join("/home","dir1","dir2")
findNremove(path,".bak")
ghostdog74
  • 307,646
  • 55
  • 250
  • 337
2

First glob them, then unlink.

Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325
1

I realize this is old; however, here would be how to do so using just the os module...

def purgedir(parent):
    for root, dirs, files in os.walk(parent):                                      
        for item in files:
            # Delete subordinate files                                                 
            filespec = os.path.join(root, item)
            if filespec.endswith('.bak'):
                os.unlink(filespec)
        for item in dirs:
            # Recursively perform this operation for subordinate directories   
            purgedir(os.path.join(root, item))
M.Markfort
  • 17
  • 2
-2

On Linux and macOS you can run simple command to the shell:

subprocess.run('rm /tmp/*.bak', shell=True)
Vitaly Zdanevich
  • 10,888
  • 6
  • 41
  • 75
  • 3
    Not a good choice in my opinion. It's not portable and it's probably more expensive due to the extra subprocess. Better to use Python APIs. – Haakon Feb 16 '18 at 21:40
  • 1
    this solution is platform dependent, whereas Python is platform agnostic. – Hamza Rashid Sep 24 '20 at 12:14