1

I got a .dat file, encoded in ANSI which contains NUL caracters (not only !).

I wan't to read it into a str object iot cut something in the whole str obtained.

But file.read() stops when the first NUL is reached.

I'm lost :)

My code :

file=open(source, mode='rt')
fulltext=file.read()
print(fulltext)

NUL is show with Notepad++ I'm on Windows XP, with PortablePython 3.2 The file is a special DAT file that can be edited with notepad as a text.

mat.viguier
  • 127
  • 1
  • 1
  • 11
  • 1
    I don't think EOF and NUL (you mean just 0?) are the same, so `file.read()` shouldn't stop. Can you show us the code you use, and tell us 1) the Python version you're using, and 2) what platform you're on? –  Nov 21 '12 at 10:21
  • 1
    It may be that pyscripter is the problem. The "Related" sidebar showed [this](http://stackoverflow.com/questions/7581963/read-stops-after-nul-character?rq=1) , which has a similar problem (socket conn. instead of a file), where the resolution was not to use pyscripter. –  Nov 21 '12 at 11:02
  • THX Evert ! I can't pay for an IDE like Wing IDE. I tried my code into a batch DOS file ... with the python.exe instead of the pythonw.exe. – mat.viguier Nov 21 '12 at 12:56
  • and it works WELL ! THX Evert. – mat.viguier Nov 21 '12 at 12:58

2 Answers2

1

If you want to read bytes like 0, chances are your file is a binary file. Read in binary mode rb instead of text mode rt.

Kos
  • 70,399
  • 25
  • 169
  • 233
  • 1
    `\x00` is how you encode NUL byte in Python string literals. The file is probably read properly so look how you're printing it. – Kos Nov 21 '12 at 10:52
  • But 13MB of data won't fit on a screen anyway – Kos Nov 21 '12 at 11:54
0

As Evert has commented in my question, the problem was focused on the use of Pyscripter ! So, this question is closed :)

mat.viguier
  • 127
  • 1
  • 1
  • 11