vegaseat

io file objects

Mar 13th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. ''' io_StringIO1.py
  2. io.StringIO from module io allows a string to be treated like a file
  3.  
  4. for images use io.BytesIO()
  5. '''
  6.  
  7. import io
  8.  
  9. # unicode u needed for Python273
  10. text = u"abcdefghijklmnopqrstuvwxyz"
  11.  
  12. # convert to a text file stream
  13. tf = io.StringIO(text)
  14. print(tf.read())  # abcdefghijklmnopqrstuvwxyz
  15.  
  16. tf.seek(10)
  17. print(tf.read())  # klmnopqrstuvwxyz
  18.  
  19. tf.seek(20)
  20. print(tf.read())  # uvwxyz
  21.  
  22. # set to the beginning of the stream
  23. tf.seek(0)
  24. # tell position
  25. print(tf.tell())  # 0
  26. # read the first 10 characters
  27. print(tf.read(10))  # abcdefghij
  28.  
  29. print(tf.tell())  # 10
  30. # read the next 10 characters
  31. print(tf.read(10))  # klmnopqrst
Advertisement
Add Comment
Please, Sign In to add comment