By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,427 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,427 IT Pros & Developers. It's quick & easy.

read(size=-1)

P: n/a
Suppose I write:

f = open("myimg.jpg")
f.read(10)
According to the docs,
-----------
read([size])

Read at most size bytes from the file....The bytes are returned as a
string object.
----------

How does python convert a byte to a string?

Aug 7 '07 #1
Share this Question
Share on Google+
2 Replies

P: n/a
7stud wrote:
Suppose I write:

f = open("myimg.jpg")
f.read(10)
According to the docs,
-----------
read([size])

Read at most size bytes from the file....The bytes are returned as a
string object.
----------

How does python convert a byte to a string?
Each byte of the file occupies one character in the string. As you could
have found out (more quickly than posting!) by actually typing your
example into an interpreter.
>>f = open('desktop01-real.jpg')
f.read(10)
'\xff\xd8\xff\xe0\x00\x10JFIF'
>>>
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Aug 7 '07 #2

P: n/a
7stud schrieb:
Suppose I write:

f = open("myimg.jpg")
f.read(10)
According to the docs,
-----------
read([size])

Read at most size bytes from the file....The bytes are returned as a
string object.
----------

How does python convert a byte to a string?
A string _is_ a sequence of bytes - there is nothing to convert.

Diez

Aug 7 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.