1

This code is working well:

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

But 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)
1
  • 1
    Are you sure that your file contains 2000 bytes of data? Commented May 9, 2011 at 11:10

1 Answer 1

2

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),)
Sign up to request clarification or add additional context in comments.

2 Comments

Filesize: 62606 Read 692 bytes
Filesize: 62606 Read 2000 bytes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.