<file object>.truncate()

Guido van Rossum guido at python.org
Sat May 20 22:24:48 EDT 2000


Bjoern Lindstroem wrote:
> 
> When I do the following:
> 
> f = open('file', 'w')
> f.truncate(3)
> f.close()
> 
> the file becomes corrupted. It has the wished size, but it is all null
> bytes.
> 
> The Library Reference says that 'the availability of this function
> depends on the operating system version'. So if it helps, I am running
> Linux 2.2.14.
> 
> If it really is the case that <file object>.truncate() is not
> available, what is the best way to get the same result?

Opening the file with mode 'w' erases its contents.  Try 'a'.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-list mailing list