Reading and writing to a file creates null characters


D

Denhua

Hi,

I've got a file which I'd like to read, modify and write.

# file contents
a
b
c
d

My script reads the file contents into a list and rotates the list and
writes it back to the same file.
Problem is that the output contains null characters. I don't know
where they are coming from.

#!/usr/bin/env python

def rotate(l):
return l[1:] + [l[0]]


f = open("/tmp/.rrd", 'r+')

lines = [ line.strip() for line in f.readlines() ]


newlist = rotate(lines)

print newlist
f.truncate(0)

f.write("\n".join(newlist))
f.close()

# output

[[email protected] html]# python rotate.py
['b', 'c', 'd', 'a']
[[email protected] html]# python rotate.py
['c', 'd', 'a', '\x00\x00\x00\x00\x00\x00\x00\x00b']
[[email protected] html]#


What's going on? Thanks for your help,
Dennis
 
Ad

Advertisements

R

Roy Smith

Denhua said:
[omitted]
f.write("\n".join(newlist))
f.close()

# output

[[email protected] html]# python rotate.py
['b', 'c', 'd', 'a']
[[email protected] html]# python rotate.py
['c', 'd', 'a', '\x00\x00\x00\x00\x00\x00\x00\x00b']
[[email protected] html]#


What's going on? Thanks for your help,
Dennis

Step 1 in debugging any problem -- try to isolate the smallest possible
test case. In your example, can you figure out if the weirdness is
happening in f.write(), or in what is being passed to f.write()? Try
breaking it down into something like:
output = "\n".join(newlist)
print output
f.write(output)
f.close()

Next, figure out if it happens whenever you write() to a file, or only
if you write() after you do a truncate().

Once you can answer those questions, you'll have a much smaller problem
to try and solve.
 
M

MRAB

Hi,

I've got a file which I'd like to read, modify and write.

# file contents
a
b
c
d

My script reads the file contents into a list and rotates the list and
writes it back to the same file.
Problem is that the output contains null characters. I don't know
where they are coming from.

#!/usr/bin/env python

def rotate(l):
return l[1:] + [l[0]]


f = open("/tmp/.rrd", 'r+')

lines = [ line.strip() for line in f.readlines() ]


newlist = rotate(lines)

print newlist
f.truncate(0)

f.write("\n".join(newlist))
f.close()

# output

[[email protected] html]# python rotate.py
['b', 'c', 'd', 'a']
[[email protected] html]# python rotate.py
['c', 'd', 'a', '\x00\x00\x00\x00\x00\x00\x00\x00b']
[[email protected] html]#


What's going on? Thanks for your help,
I think this is the relevant part of the documentation:

"""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.
"""

In other words, you also need to reset the file pointer to the start of
the file.
 
Ad

Advertisements

D

Denhua

I've got a file which I'd like to read, modify and write.
# file contents
a
b
c
d
My script reads the file contents into a list and rotates the list and
writes it back to the same file.
Problem is that the output contains null characters. I don't know
where they are coming from.
#!/usr/bin/env python
def rotate(l):
         return l[1:] + [l[0]]
f = open("/tmp/.rrd", 'r+')
lines = [ line.strip() for line in f.readlines() ]
newlist = rotate(lines)
print newlist
f.truncate(0)

# output
[[email protected] html]# python rotate.py
['b', 'c', 'd', 'a']
[[email protected] html]# python rotate.py
['c', 'd', 'a', '\x00\x00\x00\x00\x00\x00\x00\x00b']
[[email protected] html]#
What's going on? Thanks for your help,

 >
I think this is the relevant part of the documentation:

"""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.
"""

In other words, you also need to reset the file pointer to the start of
the file.

Great!
I added the line:
f.seek(0)
and that solved the problem.
Thanks to both of you for your help.
Dennis
P.s. I made assumptions about the truncate function.
I assumed that since the truncate function was clearing the file
therefore I assumed with nothing in the file that the file pointer
would be at the first position.


[email protected] html]# python rotate.py
['b', 'c', 'd', 'a']
[[email protected] html]# python rotate.py
['c', 'd', 'a', 'b']
[[email protected] html]# python rotate.py
['d', 'a', 'b', 'c']
[[email protected] html]# python rotate.py
['a', 'b', 'c', 'd']
[[email protected] html]# python rotate.py
['b', 'c', 'd', 'a']
[[email protected] html]# python rotate.py
['c', 'd', 'a', 'b']
[[email protected] html]# python rotate.py
['d', 'a', 'b', 'c']
[[email protected] html]# python rotate.py
['a', 'b', 'c', 'd']
[[email protected] html]# python rotate.py
['b', 'c', 'd', 'a']
[[email protected] html]# python rotate.py
['c', 'd', 'a', 'b']
[[email protected] html]# python rotate.py
['d', 'a', 'b', 'c']
[[email protected] html]#
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top