deleting files from a script

Greg Jorgensen gregj at pobox.com
Tue Mar 20 19:07:09 EST 2001


You can get the name of an open file from the file object's name attribute:

>>> f = open('foo')
>>> f.name
'foo'

If you always open your files with full absolute paths (rather than relative
to the current working directory) you can then retrieve that path from the
f.name attribute. With a full path, deleting the file is simple:

>>> f.close()
>>> os.remove(f.name)

The os.path module has functions for converting relative paths to absolute
paths.

"Gary" <glevenson at mediaone.net> wrote in message
news:RSRt6.2154$nd.2670928 at typhoon.ne.mediaone.net...
> Is the best way to do this to just issue a shell command using getstatus
or
> something similar? I have a file descriptor created with open. I'd rather
> not deal with constructing the entire path string if I can avoid it.

--
Greg Jorgensen / programmer, pedant, raconteur / Portland, Oregon, USA





More information about the Python-list mailing list