How to delete similar files with python?

Posted on 2009-04-24
Last Modified: 2012-05-06
I would like to delete similar files quietly with some extension under windows with python.

What I have up to now is this:

import os

os.system('del *.log')

But since this is shel command executed it always throw some stupid output if no files with this extension were found. How to delete files quietly from a python module?
Question by:koleto115
    LVL 28

    Accepted Solution

    Have a look at and See the snippet below. Uncomment the last two lines only when you are sure what is going to happen (dangerous operation).
    import glob
    import os
    path = '.' # working directory
    mask = os.path.join(path, '*.log')
    print mask
    # Just to see what it is about...
    print glob.glob(mask)
    # Process the globbed files.
    for fname in glob.glob(mask):
        print fname
        ##if os.path.isfile(fname):
        ##    os.remove(fname)    

    Open in new window


    Author Closing Comment

    LVL 28

    Expert Comment

    You are welcome. Also, have a look at the shutil module that implements a lot of functionality that is used in shell scripts (

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
    Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
    Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
    Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now