Advertisement
Guest User

Untitled

a guest
Feb 6th, 2013
2,367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. with open('hello.txt', 'w+') as f:
  2.     f.write('Hello world')
  3.     f.seek(0)
  4.     y = f.read()
  5.     print y
  6.  
  7. #with this format, the file will automatically close after the indent block. If you want to avoid #this, use the following format:
  8.  
  9. x = open('hello.txt', 'w+')
  10. x.write('Hello world')
  11. x.seek(0)
  12. z = x.read()
  13. print z
  14. x.close()
  15.  
  16. #I used w+ because I wanted to read and write from the same file without closing and reopening it. In #general, use 'a' to append (as opposed to writing over), 'r' to read, and 'w' to write.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement