Advertisement
here2share

# io_read_and_write_demo.py

May 28th, 2015
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import os
  4.  
  5. folder = "C:/pytemp/"
  6. try: os.mkdir(folder)
  7. except: pass
  8. filename = 'mytargetfile.txt'
  9. fullpath = folder + filename
  10.  
  11. # Open the file for reading
  12.  
  13. # Open the file for writing -- Note if to append the data... change the 'w' to 'a'
  14. source = open(fullpath, 'w+')
  15. source.write( "Python is a great language!\n\n-- End of Demo --\n");
  16.  
  17. # Close opened file
  18. source.close()
  19.  
  20. # Open the file for reading
  21. source = open(fullpath, 'r')
  22.  
  23. # Read the file
  24. data = source.read()
  25.  
  26. print data
  27.  
  28. # Close the output file... as for good measure
  29. source.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement