Here is my code:

# search for a file, and show all files found in that file's directory
# delete them if wanted
 
import os
import pickle
 
def file_find(folder):
    """search for a filename fname starting in folder"""
    for root, dirs, files in os.walk(folder):
        for file in files:
            # make search case insensitive
            if fname.lower() == file.lower():
                return os.path.join(root)
    return None
 
# file to search for ...
fname = raw_input("File? ")
# folder to start search ...
folder = raw_input("Directory? ")
 
result = file_find(folder)
 
if result:
    print "File found -->", result
    print "\n===============================\nFollowing are the files in the same directory\n===============================\n" 
    directory = result

    allfiles = [] #store all files found
    for root,dir,files in os.walk(directory):
            filelist = [ os.path.join(root,fi) for fi in files ] 
            for f in filelist: 
                  allfiles.append(f)
            
    fileAmount = len(allfiles)
            
    for i in allfiles:
            print "file ", i

    print "\n===============================\n"


    counter = 0
    while counter != fileAmount:

        showFile = allfiles[counter]

        counter += 1

        if counter == fileAmount:
            break
        
    deleteFiles = raw_input("Delete whole list? (y/n): ")

    if deleteFiles == "Y" or deleteFiles == "y":

        print "Sure?"
        positive = raw_input("To delete, type: 'delete', to not delete, type anything else... ")
        if positive == "delete" or positive == "Delete":

            counter = 0
            while counter != fileAmount:

                hostageFile = allfiles[counter]

                os.remove(hostageFile)

                counter += 1

                print "deleted",counter,"files..."

                if counter == fileAmount:
                    break
            raw_input("")

        else:
            print "NO files deleted..."
            raw_input("")    

    else:
        print "NO files deleted..."
        raw_input("")
        
else:
    print "File %s not found" % fname
    raw_input("")

But when it is trying to delete a directory with files that wont be easily deleted, it errors and closes.

I would like to add to the code a way to tell it to skip files that don't want to be deleted, and continue deleteing other files.

Sounds like hard program to play around with. You could really do some damage to files in your hard drive!

Yes, yes you can. It has a purpose though, my computer teacher likes to mess up the games kids install in the lab, give them a challenge if they want to play, and I made this program which he can use to sometimes delete the map files or whatnot, and since it searches for a file, even if they try to hide the game, that won't help, but sometimes it will error and just stop, I want it to skip the file it can't delete and continue on.

Is there no way to do this?

I don't have much time, so I can only really post this much. Take a look at Mouche's scheduling program:

def add_address(self):
    try:
        self.address = schedule_data[0][3]
    except IndexError:
        pass
    try:
        self.city = schedule_data[0][4]
    except IndexError:
        pass
    try:
        self.state = schedule_data[0][5]
    except IndexError:
        pass
    try:
        self.zip = schedule_data[0][6]
    except IndexError:
        pass

IndexError can be replaced with whatever type of error you're trying to catch. Hopefully somebody else with more time can explain this more fully. :)

Member Avatar for Mouche

Here is a list of all the built-in errors to "except" using the try-except statements:
http://www.python.org/doc/2.4.1/lib/module-exceptions.html

But, the easiest thing to do is run your code in a way that you can see the traceback errors. It will say something like "IndexError." Then just write whatever you want to do under the "try:" and then below it write an "except IndexError:", which executes if the code under the "try:" gets an error. Like Zonr said, you replace "IndexError" in my example with whatever error is shown by the traceback errors.

The module shutil has functions for deleting whole directory trees.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.