Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Deleting Certain File Extensions

I'm fairly new to Python, but I have gotten this code to work, and in fact, do what it's intended to do.

However, I'm wondering if there is a more efficient way to code this, perhaps to enhance the processing speed.

 import os, glob


def scandirs(path):
    for currentFile in glob.glob( os.path.join(path, '*') ):
        if os.path.isdir(currentFile):
            print 'got a directory: ' + currentFile
            scandirs(currentFile)
        print "processing file: " + currentFile
        png = "png";
        jpg = "jpg";
        if currentFile.endswith(png) or currentFile.endswith(jpg):
            os.remove(currentFile)

scandirs('C:\Program Files (x86)\music\Songs')

Right now, there are about 8000 files, and it takes quite some time to process every file and check if it indeed ends in png or jpg.

like image 914
Two Avatar asked Oct 20 '11 09:10

Two


People also ask

How do I delete a specific file extension?

To remove a file with a particular extension, use the command 'rm'. This command is very easy to use, and its syntax is something like this. In the appropriate command, 'filename1', 'filename2', etc., refer to the names, plus their full paths.

How do you delete a file extension in Python?

Given a file name, we can remove the file extension using the os. path. splitext() function. The splitext() function takes the file name as its input argument and returns a tuple containing the file name as its first element and the file extension as its second argument.

How do you remove an extension from a string in Python?

Remove the extension from a Filename using str.Use the str. rsplit() method to split the filename on a period, once, from the right. Access the list item at index 0 . The list item at index 0 will contain the filename without the extension.

How do I remove a specific file from a directory in Python?

remove() method can be used to delete a specific file, and the os. rmdir() method can be used to remove an empty directory. In addition, you can use the shutil. rmtree() method to delete a folder that contains one or more files.


2 Answers

Since you are recursing through subdirectories, use os.walk:

import os

def scandirs(path):
    for root, dirs, files in os.walk(path):
        for currentFile in files:
            print "processing file: " + currentFile
            exts = ('.png', '.jpg')
            if currentFile.lower().endswith(exts):
                os.remove(os.path.join(root, currentFile))
like image 143
unutbu Avatar answered Sep 21 '22 02:09

unutbu


If the program works and the speed is acceptable, I wouldn't change it.

Otherwise, you could try unutbu's answer.

Generally, I would leave away the

png = "png"
jpg = "jpg"

stuff as I don't see any purpose in not using the strings directly.

And better test for ".png" instead of "png".

An even better solution would be to define

extensions = ('.png', '.jpg')

somewhere centally and use that in

if any(currentFile.endswith(ext) for ext in extensions):
    os.remove(currentFile)

.

like image 39
glglgl Avatar answered Sep 19 '22 02:09

glglgl