Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ''' io_StringIO1.py
- io.StringIO from module io allows a string to be treated like a file
- for images use io.BytesIO()
- '''
- import io
- # unicode u needed for Python273
- text = u"abcdefghijklmnopqrstuvwxyz"
- # convert to a text file stream
- tf = io.StringIO(text)
- print(tf.read()) # abcdefghijklmnopqrstuvwxyz
- tf.seek(10)
- print(tf.read()) # klmnopqrstuvwxyz
- tf.seek(20)
- print(tf.read()) # uvwxyz
- # set to the beginning of the stream
- tf.seek(0)
- # tell position
- print(tf.tell()) # 0
- # read the first 10 characters
- print(tf.read(10)) # abcdefghij
- print(tf.tell()) # 10
- # read the next 10 characters
- print(tf.read(10)) # klmnopqrst
Advertisement
Add Comment
Please, Sign In to add comment