delete file
Gerhard Häring
gh at ghaering.de
Tue Jul 15 12:32:26 EDT 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"):
os.remove("foo")
or just catch the error, with something like:
try:
os.remove("foo")
except OSError, detail:
if detail.errno != 2:
raise
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
grateful.
-- Gerhard
More information about the Python-list
mailing list