943,535 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1734
  • Python RSS
Nov 1st, 2006
0

Prevent Code Errors?

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
Nov 1st, 2006
0

Re: Prevent Code Errors?

Sounds like hard program to play around with. You could really do some damage to files in your hard drive!
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Nov 1st, 2006
0

Re: Prevent Code Errors?

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.
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
Nov 3rd, 2006
0

Re: Prevent Code Errors?

Is there no way to do this?
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
Nov 3rd, 2006
0

Re: Prevent Code Errors?

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.
Reputation Points: 10
Solved Threads: 3
Newbie Poster
Zonr_0 is offline Offline
16 posts
since Oct 2006
Nov 4th, 2006
0

Re: Prevent Code Errors?

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/...xceptions.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.
Featured Poster
Reputation Points: 83
Solved Threads: 39
Posting Whiz in Training
LaMouche is offline Offline
263 posts
since Oct 2006
Nov 11th, 2006
0

Re: Prevent Code Errors?

The module shutil has functions for deleting whole directory trees.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Event handling across panels issues...
Next Thread in Python Forum Timeline: Color Dictionary





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC