Write \x1a to binary file

Bengt Richter bokr at oz.net
Mon Jul 15 15:55:09 EDT 2002


On Mon, 15 Jul 2002 16:37:07 +0200, "Harald Schneider" <h_schneider at marketmix.com> wrote:

>Hi,
>
>I've got a problem with writing "\x1A" to a binary file on Win32 platform.
>I think due tue an issue wiht popen() this terminates the output stream. Is
>there any solution for this ?
>
Why are you using popen? Why not

 Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
 Type "help", "copyright", "credits" or "license" for more information.
 >>> f=file('x1a_test.txt','wb')
 >>> f.write('The character in square brackets should be \\x1A: [\x1A]\r\n')
 >>> f.close()

We can verify the result.
 >>> f=file('x1a_test.txt','rb')
 >>> s=f.read()
 >>> s
 'The character in square brackets should be \\x1A: [\x1a]\r\n'
 >>> print s
 The character in square brackets should be \x1A: [?]

 >>> s.find('[')
 49
 >>> s[50]
 '\x1a'

(On the console window (NT4) the character in square brackets above renders
as a right arrow. Pasted into my newsreader is shows as a question mark).

Note the 'b' in the mode, necessary for windows. Otherwise,
if we read the file we just wrote, we get
:
 >>> f=file('x1a_test.txt','r')
 >>> s=f.read()
 >>> s
 'The character in square brackets should be \\x1A: ['
 >>> print s
 The character in square brackets should be \x1A: [
 >>> s.find('[')
 49
 >>> s[50]
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
 IndexError: string index out of range

I.e., '\x1a' is Ctrl-Z which is EOF in windows text mode.

Just to complete the picture, note what happens if we write
without the 'b' and read its binary data with 'rb':

 >>> f=file('x1a_test.txt','w')
 >>> f.write('The character in square brackets should be \\x1A: [\x1A]\r\n')
 >>> f.close()
 >>> f=file('x1a_test.txt','rb')
 >>> s=f.read()
 >>> s
 'The character in square brackets should be \\x1A: [\x1a]\r\r\n'
 >>> print s
 The character in square brackets should be \x1A: [?] 

 >>> s.find('[')
 49
 >>> s[50]
 '\x1a'

It looks like the only cooking that was done was to change \n to \r\n for
windows (thus getting the \r\r\n, since \r was alrady there), but the \x1a
was apparently ignored and passed though in writing the output.

Regards,
Bengt Richter



More information about the Python-list mailing list