473,409 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

read(size=-1)

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
2 1162
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.