954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Prevent Code Errors?

Here is my code:
[php]# 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("")[/php]
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.

Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

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

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

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.

Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

Is there no way to do this?

Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

I don't have much time, so I can only really post this much. Take a look at Mouche's scheduling program:
[PHP]
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
[/PHP]


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. :)

Zonr_0
Newbie Poster
16 posts since Oct 2006
Reputation Points: 10
Solved Threads: 3
 

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.

LaMouche
Posting Whiz in Training
269 posts since Oct 2006
Reputation Points: 83
Solved Threads: 39
 

The module shutil has functions for deleting whole directory trees.

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You