Does ftruncate work?

Jason Orendorff jason at jorendorff.com
Fri Jan 11 00:05:43 EST 2002


> Can somebody tell me what's wrong here?
> 
> [pw at slink pw]$ touch foo
> [pw at slink pw]$ python
> Python 2.1.1+ (#1, Jan  8 2002, 00:37:12) 
> [GCC 2.95.4 20011006 (Debian prerelease)] on linux2
> Type "copyright", "credits" or "license" for more information.
> >>> import os
> >>> print os.ftruncate.__doc__
> ftruncate(fd, length) -> None
> Truncate a file to a specified length.
> >>> f = open('foo', 'rw')
> >>> os.ftruncate(f, 1)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: an integer is required

The "fd" in the docstring indicates that this function wants a
file descriptor.  That is, the fileno() of the file.

  os.ftruncate(f.fileno(), 1)

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list