This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Read from file aborted
Type: behavior Stage:
Components: Windows Versions: Python 3.3, Python 2.7
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: Alex.Grinko, eryksun
Priority: normal Keywords:

Created on 2014-03-30 18:02 by Alex.Grinko, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
sample.txt Alex.Grinko, 2014-03-30 18:02 Sample file with lines and special character
Messages (2)
msg215190 - (view) Author: Alex Grinko (Alex.Grinko) Date: 2014-03-30 18:02
When reading from text file on Windows Python ends loop on character 0x1A
sample.py:
number = 0
with open("sample.txt",'r') as f:
    for line in f:
        number += 1
	print line
print "File has 8 lines, but Python could read only ",number
msg215203 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2014-03-30 21:26
AFAIK, this doesn't affect Python 3 under normal circumstances. A file could be manually set to text mode by calling msvcrt.setmode(f.fileno(), os.O_TEXT), but that's out of the norm.

In Python 2, on Windows interpreting ctrl+z (0x1a) as end-of-file is normal behavior in text mode. Read the remarks about [t]ext mode in the description of Microsoft's fopen implementation:

http://msdn.microsoft.com/en-us/library/yeby3zcb%28v=vs.90%29.aspx

If you use Python's universal newlines mode, e.g. open('sample.txt', 'rU'), then the underlying file is opened in binary mode and therefore will not interpret ctrl+z as the end-of-file marker.
History
Date User Action Args
2022-04-11 14:58:01adminsetgithub: 65303
2014-03-31 06:35:53Alex.Grinkosetstatus: open -> closed
resolution: works for me
2014-03-30 21:26:33eryksunsetnosy: + eryksun
messages: + msg215203
2014-03-30 18:02:23Alex.Grinkocreate