Newbie question: files..

Thomas Wouters thomas at xs4all.nl
Thu Oct 21 10:13:36 EDT 1999


On Wed, Oct 20, 1999 at 09:25:43PM +0000, Wesley C. wrote:
> jesse's shorter 1-liner:
> 
> print open('testfile.txt').read()
> 
> ...is fine too, although it's always a good idea to close
> the file.  in this example, we've lost our file handle
> and must wait for the garbage collector to cleanup after
> us.  if the file is deleted while it is still opened, it
> may still show up on a directory listing because the file
> ref. count hasn't gone down to zero yet.

Actually, no, thank god it wont. Files on filesystems work remarkably like
python objects ;) A file as listed in a directory listing isn't actually a
file, it's a _reference_ to a file. Deleting a file only deletes the
reference to it, not necessarily the file itself. Once a file has no
references, its space gets reclaimed. However, you also create a (sort of)
reference to a file by open()ing it, so as long as you have it open, it wont
be reclaimed, even if it's deleted from the original directory. You can't
get it back, though, short of copying all the data while you still have it
open.

Actually, since linux 2.1.something, you can 'retrieve' a file that's been
deleted, but is still open by some process, by copying /proc/<pid>/fd/<fd>
to a new (or old) location. Note that you wont get any 'pending changes'
that haven't been written to disk yet, you get the file back exactly as if
it hadn't been deleted yet. If you insist on retrieving the file as it
'would have been' if you hadn't deleted it, and the program had finished,
you have to convince the program to flush it's buffers, but wait before
exiting. I dont think there is a 'relink' method yet, as /proc is supposed
to be a different filesystem.

For the curious:

Python 1.5.2+ (#4, Aug 25 1999, 08:17:00)  [GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> file = open("/tmp/tmpfile",'r+')
>>> import os
>>> os.getpid()
19504

>>> file.readline()
'Our chief weapon is suprise! Suprise and fear!\012'

>>> file.readline()
'Our two weapons are Suprise and Fear and an almost fanatical devotion to the pope!\012'

>>> file.readline()
'Amongst our weaponry are such elements as suprise and fear and an almost fanatical devotion to the pope and nice red suits and oh bugger.\012'

>>> file.write("I didn't expect some kind of spanish inquisition!\n")
>>> 
Suspended

centurion:~ > cd /proc/19504/fd

centurion:/proc/19504/fd > ls -l

total 0
lrwx------   1 thomas   thomas         64 Oct 21 16:07 0 -> /dev/pts/15
lrwx------   1 thomas   thomas         64 Oct 21 16:07 1 -> /dev/pts/15
lrwx------   1 thomas   thomas         64 Oct 21 16:07 2 -> /dev/pts/15
lrwx------   1 thomas   thomas         64 Oct 21 16:07 3 -> /tmp/tmpfile

centurion:/proc/19504/fd > rm /tmp/tmpfile 
centurion:/proc/19504/fd > ls -l 3
lrwx------   1 thomas   thomas         64 Oct 21 16:07 3 -> /tmp/tmpfile (deleted)

centurion:/proc/19504/fd > cp 3 /tmp/tmpfile2
centurion:/proc/19504/fd > cat /tmp/tmpfile2
Our chief weapon is suprise! Suprise and fear!
Our two weapons are Suprise and Fear and an almost fanatical devotion to the pope!
Amongst our weaponry are such elements as suprise and fear and an almost fanatical devotion to the pope and nice red suits and oh bugger.

centurion:/proc/19504/fd > fg
python (wd: ~)
file.flush()
>>> 
Suspended

centurion:/proc/19504/fd > cp 3 /tmp/tmpfile2
centurion:/proc/19504/fd > cat /tmp/tmpfile2 
Our chief weapon is suprise! Suprise and fear!
Our two weapons are Suprise and Fear and an almost fanatical devotion to the pope!
Amongst our weaponry are such elements as suprise and fear and an almost fanatical devotion to the pope and nice red suits and oh bugger.
I didn't expect some kind of spanish inquisition!

toys-toys-toys'ly y'rs, 
-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list