2

When executing the following code, after the second read, file gets filled with zero until being 4096 bytes large. I can't figure out why :

f = open("file.txt", "w+")
f.read()
# gives ''
f.write("Hello")
f.read()
# filled with \x00,\x00
f.close()
# file is 4096 bytes large...
Lapin-Blanc
  • 1,945
  • 1
  • 17
  • 26

2 Answers2

3

Best way to solve your problem: don't mix read() and write().

Otherwise: after the write(), use seek() before the second read() to read your file from the beginning:

f = open("file.txt", "w+")
print f.read()      # prints ''
f.write("Hello")
f.seek(0)
print f.read()      # print 'Hello'
f.close()
sloth
  • 99,095
  • 21
  • 171
  • 219
  • @PaoloMoretti True. Also note that the behaviour of the OPs code depends on the OS/filesystem you use. While the code would probably work on Linux with Ext3/Ext4, using it on Win7 with NTFS will result in a bunch of garbage written to the file. – sloth Oct 26 '12 at 08:40
2

This is probably related to the filesystem and/or partition. I'm not fully up to speed on the details, but it is likely that 4096 bytes (i.e. exactly 4Kb) is the minimum size of a file in your filesystem and partition. AFAIK, the partition has a block size (often 4096 bytes), and files can only be allocated to whole blocks. So a file is always a multiple of 4096 bytes. I think that some filesystems have a way around this (e.g. reiserfs) by combining small files.

aquavitae
  • 17,414
  • 11
  • 63
  • 106