delete will assure file is deleted?

andreas at kostyrka.org andreas at kostyrka.org
Tue Apr 26 15:24:30 EDT 2005


On Tue, Apr 26, 2005 at 03:13:20PM -0400, Jeremy Bowers wrote:
> On Tue, 26 Apr 2005 03:40:16 -0700, ajikoe at gmail.com wrote:
> 
> > 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.
> 
> I would be fairly confident in asserting that assuming the file is there,
> you have permissions, etc., basically that the call succeeds, that the
> file will be gone.
Not exactly. The system call is called remove not by accident. It's not
called delete. So for example if you have a file with multiple names (so called
hard links) the file will not be gone after os.remove('file')

> 
> os.remove, as the module name implies, tells the OS to do something. I
> would consider an OS that returned from a "remove" call, but still let you
> access that file, highly broken. 
Well, it has been the normal semantics with Unix for decades. Actually it's
the normal way to create temporary files that will be cleanuped when the program
exits:
f = open("temp")
os.remove("temp")
# now use f

f.close() # frees the temporary file
sys.exit(0) # exit is an implicit close too.

Andreas



More information about the Python-list mailing list