[plug] python head+wall issue

Craig Ringer craig at postnewspapers.com.au
Mon Dec 29 19:53:40 WST 2003


hi folks

I've been trying to figure out why I can't truncate a file in Python. 
I'm sure it's obvious, but if someone could supply the appropriate 
clue-bat that'd be handy.

Given a file open r/w eg:

 >> f = file("/tmp/test","rw")

if I try to truncate this (non-zero-length) file:

 >> f.truncate()
or
 >> f.truncate(0)
or
 >> f.truncate(0L)

it fails with:

Traceback (most recent call last):
   File "<stdin>", line 1, in ?
IOError: [Errno 22] Invalid argument

which strikes me as odd. If I trace the Python process, I see:

fstat64(3, {st_mode=S_IFREG|0664, st_size=40, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 
0) = 0xbf335000
_llseek(3, 0, [0], SEEK_CUR)            = 0
ftruncate64(3, 0)                       = -1 EINVAL (Invalid argument)
write(2, "Traceback (most recent call last"..., 35) = 35

the file:

$ ls -l /tmp/test
-rw-rw-r--    1 craig    craig          40 Dec 29 19:48 /tmp/test

is a normal, boring file.

If I

 >> f = file("/tmp/test","w")
 >> f.close()

then

$ ls -l /tmp/test
-rw-rw-r--    1 craig    craig           0 Dec 29 19:48 /tmp/test

the file is truncated, as it should be.

*bangs* *head* *against* *wall*

Craig Ringer




More information about the plug mailing list