[Tutor] file read and write
Kent Johnson
kent37 at tds.net
Tue Sep 11 13:45:38 CEST 2007
le dahut wrote:
> I noticed that it is possible to write this :
> """
> file('/tmp/myfile', 'w').write('Hello world\n')
ISTM I have had trouble with this. I always explicitly close a file that
is open for writing.
> contnt = file('/tmp/sourcefile').read()
I use this often but never in any kind of loop where multiple files are
being read.
Note that the behaviour of the garbage collector is
implementation-dependent. It is an implementation detail of CPython that
GC is implemented with reference counting and objects are disposed when
there are no longer any references to them. Jython uses Java's GC which
does not behave the same way; PyPy and IronPython may have different GC
as well.
So if you want your code to be portable between implementations of
Python you should not rely on this behaviour.
Kent
More information about the Tutor
mailing list