Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

python - cannot read ascii character 26?

I wrote a stream in a file in text mode.

# python code
f = open("somewhere in my computer","w")
f.write("Hello
World")
f.write(chr(26)) # writing ascii character #26 to file 
f.write("hhh")
f.close()

I cannot read bytes after ASCII character #26. I know i should open file with binary mode. Is ascii character #26 EOF character. As you know there is not such thing , e.i there is no EOF character. So what is problem? Is this an operating system dependent issue? (I try this in Microsoft windows).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem is because of the windows. 0x1A is Ctrl-Z, and DOS used that as an end-of-file marker. Python uses the Windows CRT function _wfopen, which implements the "Ctrl-Z is EOF" semantics. If your file wasn't an exact multiple of 128 bytes, you needed a way to mark the end of the text. This article implies that the selection of Ctrl-Z was based on an even older convention used by DEC.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...