Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python writing to file prepends null characters

Tags:

python

file-io

I am trying to maintain a file of an offset value (file.tell) so that on restarts the process can skip those many characters.

So I have a sequence of truncate/write/flush. But every time it puts null characters at the top of file. The pattern appears that the number of null characters equal the length of the previous file contents.

I have reproduced the session below. I will appreciate your help in figuring out how to not get these nulls.

Thanks a lot.

dinesh@c1 ~/lab $ python
Python 2.6.8 (unknown, Jul 16 2013, 14:48:55)
[GCC 4.3.4 [gcc-4_3-branch revision 152973]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os,fcntl
>>> f=os.open("ha.offset", os.O_RDWR, 0744)
>>> fcntl.lockf( f, fcntl.LOCK_EX | fcntl.LOCK_NB )
>>> fd = os.fdopen(f, "w+")
>>> msg="first=123\nsecond=234\n"
>>> fd.truncate(0)
>>> fd.write(msg)
>>> fd.flush()
>>>
[1]+  Stopped                 python
dinesh@c1 ~/lab $ wc ha.offset
 2  2 21 ha.offset
dinesh@c1 ~/lab $ fg
python
>>> msg="first=987\nsecond=9877\n"
>>> fd.truncate(0)
>>> fd.write(msg)
>>> fd.flush()
>>>
[1]+  Stopped                 python
dinesh@c1 ~/lab $ wc ha.offset
 2  2 43 ha.offset
dinesh@c1 ~/lab $ od -c ha.offset
0000000  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000020  \0  \0  \0  \0  \0   f   i   r   s   t   =   9   8   7  \n   s
0000040   e   c   o   n   d   =   9   8   7   7  \n
0000053
dinesh@c1 ~/lab $ fg
python
msg="first=1"
msg="first=1"
>>> fd.truncate(0)
>>> fd.write(msg)
>>> fd.flush()
>>>
[1]+  Stopped                 python
dinesh@c1 ~/lab $ !od
od -c ha.offset
0000000  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
*
0000040  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0   f   i   r   s   t
0000060   =   1
0000062
like image 852
Dinesh Avatar asked Feb 21 '26 12:02

Dinesh


1 Answers

The docs for truncate state that the position in the file is maintained:

file.truncate([size])

Truncate the file’s size. If the optional size argument is present, the file is truncated to (at most) that size. The size defaults to the current position. The current file position is not changed. Note that if a specified size exceeds the file’s current size, the result is platform-dependent: possibilities include that the file may remain unchanged, increase to the specified size as if zero-filled, or increase to the specified size with undefined new content. Availability: Windows, many Unix variants.

So I suspect you need to just fd.seek(0) somewhere between the first and second writes.

like image 146
Peter Gibson Avatar answered Feb 24 '26 00:02

Peter Gibson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!