delete will assure file is deleted?

Mike Meyer mwm at mired.org
Tue Apr 26 22:33:52 EDT 2005


Dennis Lee Bieber <wlfraed at ix.netcom.com> writes:

> On 26 Apr 2005 03:40:16 -0700, "ajikoe at gmail.com" <ajikoe at gmail.com>
> declaimed the following in comp.lang.python:
>
>> Hello Mike,
>> I have to know this topic otherwise my program has to check whether the
>> file / files are already deleted and this is a little bit messy.
>>
> 	Ah, but this requirement is different from the one you
> originally asked.
>
> 	In either event, the best solution is probably to wrap the call
> with a try block...
>
> 	try:
> 		os.remove(fileName)
> 	except <need to determine what errors can be raised>:
> 		# do whatever you need for the error
> 	except <other error>:
> 		# do whatever this error needs...
>
> 	If the error is that the file didn't exist to be deleted, you
> can probably use a "pass" as the except processing.

This is just a little bit tricky. os.remove (on FreeBSD 5-STABLE,
anyway) throws an OSError exception if it doesn't have permission to
remove the file, *or* if the file doesn't exist. You have to examine
the exception for it's value, which is the result of a strerror
call. I believe that the result of strerror is platform dependent.

      <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list