1

Possible Duplicate:
Weird behavior when writing and reading file

When I try to write into a file and then read it, I get an unexpected result.

My code is:

f=open("z.txt","w+")
f.write("Hello")
content=f.read()
print content
f.close()

The outputted file is:

Hellolף  ן (I11 (S'QUEUE' p1 (S'exec' p2 S'runcode' p3 (cidlelib.rpc unpickle_code p4 (S'c\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00@\x00\x00\x00s6\x00\x00\x00e\x00\x00d\x00\x00d\x01\x00\x83\x02\x00Z\x01\x00e\x01\x00j\x02\x00d\x02\x00\x83\x01\x00\x01e\x01\x00j\x03\x00\x83\x00\x00Z\x04\x00e\x01\x00j\x05\x00\x83\x00\x00\x01d\x03\x00S(\x04\x00\x00\x00s\x05\x00\x00\x00z.txts\x02\x00\x00\x00w+t\x05\x00\x00\x00HelloN(\x06\x00\x00\x00t\x04\x00\x00\x00opent\x01\x00\x00\x00ft\x05\x00\x00\x00writet\x04\x00\x00\x00readt\x07\x00\x00\x00contentt\x05\x00\x00\x00close(\x00\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00s\x17\x00\x00\x00C:/Users/x/Desktop/zt\x08\x00\x00\x00\x01\x00\x00\x00s\x06\x00\x00\x00\x0f\x01\r\x01\x0c\x01' tRp5 tp6 (dp7 ttp8 tp9 . ‘2ר ְ ז׀l Ak !
€qg ¸Ck PCk $ ְBkאBkנBk 8Ck
xCk XBkxBk °Ck׀CkנCkDk
@‘ (Ck˜Ck kצ Ck Ck
ְpg DkDֽ˜F ! €qg + ˜Ck
(Dk @k @k xDk ר?k @f HDkhDkpDk ˜Dk
נDkEk0EkPEk ְ” kצ
ְpg ! ˜F ,
״Dk

@k @k                      ר?k

What am I doing wrong?

Community
  • 1
  • 1
Lior
  • 5,841
  • 9
  • 32
  • 46
  • 5
    First of all, your `.read()` will have returned an empty string, since the write put the file position at the end. Secondly, the `w+` mode would have truncated the file, causing the file to be empty when you opened it. The extra data you see was added *after* this program was run. What else have you done with this file? Nothing in your code shown would have added the extra data, but the [`pickle` module](http://docs.python.org/2/library/pickle.html) output looks a lot like what you pasted here. – Martijn Pieters Nov 22 '12 at 15:46

2 Answers2

2

I believe this is a Windows issue, to do with filesystem block sizes. If you .flush() the .write() before you .read(), it will work fine. I can reproduce it on Win7.

(I think what is happening is that Windows allocates your file in blocks of 4KB, so writing "Hello" bumps you up to the next block size. If you then .read() you get random gibberish from the rest of the block, because Python hasn't had a chance to deal with it yet. If you .flush() before .read()ing, Python writes out the file properly and then you just get an empty string.)

Katriel
  • 120,462
  • 19
  • 136
  • 170
  • So in fact it's a bug on python's memory handling? – Lior Nov 22 '12 at 16:13
  • @user1718294 It's not a bug. Python `file.read()` is simply a wrapper for the underlying `fread()` C function, and [C standards](http://stackoverflow.com/a/2230732/63011) says you should always flush (or seek) your stream to get the correct result. – Paolo Moretti Nov 22 '12 at 16:48
  • I wonder whether it is sensible to automatically flush a file that has been written before reading it. – Katriel Nov 22 '12 at 16:58
0
f=open("z.txt","w+")
f.write("Hello")
f.seek(0)
content=f.read()
print content
f.close()

I was missing
f.seek(0)

Lior
  • 5,841
  • 9
  • 32
  • 46
  • That doesn't explain your extra pickle data, but if this answers your question.. – Martijn Pieters Nov 22 '12 at 15:52
  • @Martijn Pieters Yes, it solved my problem, now the file also contains the text "Hello" and not the other gibberish stuff. – Lior Nov 22 '12 at 15:54
  • So where did the gibberish come from? It still seems a mystery; it shouldn't have been there at all. – Will Vousden Nov 22 '12 at 15:56
  • @Will Vousden That's why I asked the question in the first place, I had no idea where did the gibberish came from. – Lior Nov 22 '12 at 16:11