Advertisement
Felanpro

File I/O

Mar 3rd, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.52 KB | None | 0 0
  1. import os
  2.  
  3. #Overwrite or create a file for writing
  4. test_file = open("test.txt", "wb")
  5.  
  6. #Get the file mode used
  7. print(test_file.mode)
  8.  
  9. #Get the files name
  10. print(test_file.name)
  11.  
  12. #Write text to a file with a newline
  13. test_file.write(bytes("Write me to the file\n", 'UTF-8'))
  14.  
  15. #Close the file
  16. test_file.close()
  17.  
  18. #Opens a file for reading and writing
  19. test_file = open("test.txt", "r+")
  20.  
  21. #Read text from the file
  22. text_in_file = test_file.read()
  23.  
  24. print(text_in_file)
  25.  
  26. #Delete the file
  27. os.remove("test.txt")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement