delete file

Gerhard Häring gh at
Tue Jul 15 18:32:26 CEST 2003

lamar_air wrote:
> I am using this remove method and it works well as long as the file is
> there
> os.remove("C:\Inetpub\wwwroot\Cgi-bin\output.txt")
> If the file doesn't exist then i get an error that the file is not
> found.  I want to stay away from using
> f2=open('C:\Inetpub\wwwroot\Cgi-bin\output.txt', 'w') so how can i
> check if the file exists first?

if not os.path.exists("foo"):

or just catch the error, with something like:

except OSError, detail:
     if detail.errno != 2:

This will ensure that only the "file not found case" is ignored, not for 
example the "insufficient permission" case. Not that I know where this 
error numbers comes from, I just experimented ;-) If anybody could tell 
me where I can find those error numbers without RTFMing myself I'd be 

-- Gerhard

More information about the Python-list mailing list