开发者

Python: problems with read() when it is reading larger data (binary mode)

开发者 https://www.devze.com 2023-03-03 17:48 出处:网络
This code is working well: f = open(\"C:/Dokumente und Einstellungen/schnei17/Desktop/a.txt\", \"r\")

This code is working well:

f = open("C:/Dokumente und Einstellungen/schnei17/Desktop/a.txt", "r") 
f.seek(0) 
print f.read(200)

Bu开发者_Go百科t here read() doesn't work properly:

print f.read(2000)

The output is very short now. Is this a bug? The file contains unicode-data. Thanks in advance.

Solution:

f = open("C:/Dokumente und Einstellungen/schnei17/Desktop/a.txt", "rb") 
f.seek(0) 
print f.read(200)


What does this produce?

import os
filename = "C:/Dokumente und Einstellungen/schnei17/Desktop/a.txt"
print "Filesize: %s" % (os.path.getsize(filename),)
f = open(filename, "r")
data = f.read(2000)
print "Read %s bytes" % (len(data),)

Filesize: 62606 Read 692 bytes

And changing the read mode to binary?

import os
filename = "NewProv.txt"
print "Filesize: %s" % (os.path.getsize(filename),)
f = open(filename, "rb")
data = f.read(2000)
print "Read %s bytes" % (len(data),)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号