<file object>.truncate()

David Goodger dgoodger at bigfoot.com
Sat May 20 22:29:37 EDT 2000


on 2000-05-20 19:36, Bjoern Lindstroem (magog at swipnet.se) 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.

I assume that you want to open an existing file and truncate it, leaving the
existing (3 bytes of) data.

>From the Python Library Reference (1.5.2p2), section 2.8, Built-in
Functions:

    open (filename[, mode[, bufsize]])

    Return a new file object ... The first two arguments are the same
    as for stdio's fopen(): ... mode indicates how the file is to be
    opened: 'r' for reading, 'w' for writing (truncating an existing
    file), and 'a' opens it for appending (which on some Unix systems
    means that all writes append to the end of the file, regardless of
    the current seek position).

    Modes 'r+', 'w+' and 'a+' open the file for updating (note that
    'w+' truncates the file). ...

I think your problem may be in your interpretation of the word "truncating"
under the 'w' mode above. Since Python's open() modes are the same as
stdio's fopen() in C, the following is relevant. From The C Programming
Language, Second Edition (Kernighan & Ritchie), Appendix B, section B1.1,
(<stdio.h>) File Operations:

    FILE *fopen ...
    
    ... Legal values for mode include:
    
        ...
        "w"        create text file for writing; discard previous
                contents if any
        ...
        "w+"    create text file for update; discard previous
                contents if any
        "a+"    append; open or create text file for update, writing
                at end
    
    Update mode permits reading and writing the same file; ...

So in the Python docs, "truncating" is used, whereas in C, it's "discard
previous contents if any". A bit misleading. Basically, modes "w" and "w+"
both create a *new* file, clobbering/erasing any existing file with the same
name. So your f.truncate(3) makes perfect sense: it sets the file length of
a *new* file to 3 bytes, and since it *is* a new file, it's reasonable
(downright friendly, in my opinion!) to put nulls in there.

I think what you want is to use the mode "a+" for a text file, or "a+b" for
a binary file:

    >>> f1 = open("tmpfile", "w")
    >>> f1.write("123456789")
    >>> f1.close()
    >>> print open("tmpfile", "r").read()
    123456789
    >>> f2 = open("tmpfile", "a+")
    >>> f2.truncate(3)
    >>> f2.close()
    >>> print open("tmpfile", "r").read()
    123

-- 
David Goodger    dgoodger at bigfoot.com    Open-source projects:
 - The Go Tools Project: http://gotools.sourceforge.net
 (more to come!)




More information about the Python-list mailing list