Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


How to delete similar files with python?

Posted on 2009-04-24
Medium Priority
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
  • 2
LVL 29

Accepted Solution

pepr earned 2000 total points
ID: 24222965
Have a look at http://docs.python.org/library/glob.html#glob.glob and http://docs.python.org/library/os.html#os.remove. 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

ID: 31574130
LVL 29

Expert Comment

ID: 24223449
You are welcome. Also, have a look at the shutil module that implements a lot of functionality that is used in shell scripts (http://docs.python.org/library/shutil.html).

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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…
Suggested Courses
Course of the Month15 days, 19 hours left to enroll

580 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