Guest User

Untitled

a guest
Nov 23rd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. import json
  2.  
  3. filename = "helloworld.txt"
  4.  
  5. # reading the whole file at once
  6. with open(filename) as file_object:
  7. contents = file_object.read()
  8. print(contents)
  9.  
  10. print("READING LINE BY LINE")
  11. # reading the file line by line
  12. with open(filename) as file_object:
  13. lines = file_object.readlines()
  14. print(lines)
  15. for line in lines:
  16. print(line.rstrip('\n'))
  17.  
  18.  
  19. # writing a file
  20. with open('filetowrite.txt','w') as file_object:
  21. file_object.write('Bye World. I am using Python to write a file')
  22.  
  23.  
  24. # appending file
  25. with open('appendingToFile.txt','a') as file_object:
  26. file_object.write('Bye World')
  27. file_object.write('\n')
  28.  
  29.  
  30. # Writing JSON to a file using json.dump
  31. with open('users.json','w') as file_object:
  32. dictionary = {'firstname':'John','lastname':'Azam'}
  33. json.dump(dictionary,file_object)
  34.  
  35.  
  36. # reading JSON from file using json.load
  37. with open('users.json') as file_object:
  38. dictionary = json.load(file_object)
Add Comment
Please, Sign In to add comment