Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. # Writing to a file in Python
  2. # This was tested on Python 2.7.
  3.  
  4. # path - is a string to desired path location. The file does
  5. # not have to exist.
  6. # content - is a string with the file content.
  7. def writeToFile(path, content):
  8. file = open(path, "w")
  9. file.write(content)
  10. file.close()
  11.  
  12.  
  13. PATH_TO_MY_FILE = './example.txt'
  14. CONTENT_FOR_MY_FILE = 'Example\nThis is on line 2 of a text file.\n\nThe end.'
  15.  
  16. writeToFile(PATH_TO_MY_FILE, CONTENT_FOR_MY_FILE)
  17.  
  18. # Run in terminal using:
  19. # python write-to-file.py
  20. #
  21. # It will create a file called `example.txt` and the contents will look like (minus the number signs):
  22. # Example
  23. # This is on line 2 of a text file.
  24. #
  25. # The end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement