Advertisement
tuomasvaltanen

Untitled

Nov 30th, 2022 (edited)
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.19 KB | None | 0 0
  1. # lecture 30.11.2022, file management in Python
  2. print("Welcome!")
  3.  
  4. # NEW TEXT FILE - weekdays.txt
  5.  
  6. Monday
  7. Tuesday
  8. Wednesday
  9. Thursday
  10. Friday
  11. Saturday
  12. Sunday
  13.  
  14. # NEW FILE
  15.  
  16. # open a connection to our file
  17. file_handle = open("weekdays.txt", "r")
  18.  
  19. # read all contents to a variable and print
  20. content = file_handle.read()
  21. print(content)
  22.  
  23. # always remember to close the connection in the end
  24. # to avoid strange file locking bugs
  25. file_handle.close()
  26.  
  27. # NEW FILE
  28.  
  29. # open a connection to our file
  30. file_handle = open("weekdays.txt", "r")
  31.  
  32. counter = 1
  33.  
  34. while True:
  35.     # keep on getting a single line from file
  36.     # as the are lines still left
  37.     line = file_handle.readline()
  38.     print(f"{counter}. {line}")
  39.  
  40.     counter = counter + 1
  41.  
  42.     # no more lines left in file => break
  43.     if not line:
  44.         break
  45.  
  46. # always remember to close the connection in the end
  47. # to avoid strange file locking bugs
  48. file_handle.close()
  49. # NEW FILE
  50.  
  51. # open a connection to our file
  52. file_handle = open("weekdays.txt", "r")
  53.  
  54. # read all contents to a variable and print
  55. content = file_handle.read()
  56. lines = content.split("\n")
  57.  
  58. # because lines is just a Python list, we can use loops
  59. for line in lines:
  60.     print(line)
  61.  
  62. print()
  63.  
  64. # or we can do a numbered just like before with products
  65. amount = len(lines)
  66.  
  67. for index in range(amount):
  68.     line = lines[index]
  69.     print(f"{index + 1}. {line}")
  70.  
  71.  
  72. # always remember to close the connection in the end
  73. # to avoid strange file locking bugs
  74. file_handle.close()
  75.  
  76. # NEW FILE
  77.  
  78. # open file for writing
  79. file_handle = open("memo.txt", "w", encoding="utf-8")
  80.  
  81. # ask user for text, write it to file
  82. text = input("Give some text:\n")
  83. file_handle.write(text)
  84.  
  85. # always remember to close the connection in the end
  86. # to avoid strange file locking bugs
  87. file_handle.close()
  88.  
  89. # NEW FILE
  90.  
  91. # open file for writing, use append instead of "w"
  92. file_handle = open("memo.txt", "a", encoding="utf-8")
  93.  
  94. # ask user for text, write it to file
  95. text = input("Give some text:\n")
  96.  
  97. # remember add new lines if you don't want
  98. # to append text on the same line
  99. file_handle.write(text + "\n\n")
  100.  
  101. # always remember to close the connection in the end
  102. # to avoid strange file locking bugs
  103. file_handle.close()
  104.  
  105. # NEW JSON FILE -> app_data.json
  106.  
  107. {
  108.   "name": "Rovaniemi",
  109.   "population": 62933,
  110.   "county": "Lapland"
  111. }
  112.  
  113. # NEW FILE
  114.  
  115. import json
  116. import var_dump as vd
  117.  
  118. # get the JSON data from the file
  119. file_handle = open("app_data.json", "r")
  120. content = file_handle.read()
  121. file_handle.close()
  122.  
  123. # convert JSON data (which is text) into Python data
  124. city = json.loads(content)
  125.  
  126. # city is just a Python dictionary
  127. print(city['name'])
  128. print(city['county'])
  129.  
  130. # compare the vardumps
  131. # vd.var_dump(content)
  132. # print()
  133. # vd.var_dump(city)
  134.  
  135. # NEW FILE
  136.  
  137. import json
  138.  
  139. # create a dictionary, for example, a phone
  140. phone = {
  141.     "name": "Nokia 3310",
  142.     "release_year": 2000,
  143.     "battery": "1000mAh",
  144.     "camera": False,
  145.     "weight": 133
  146. }
  147.  
  148. # if you want to save JSON in prettier format
  149. # try adding parameter indent = 4
  150. content = json.dumps(phone)
  151.  
  152. # open file for writing, write the JSON text
  153. # inside the file
  154. file_handle = open("phone.json", "w")
  155. file_handle.write(content)
  156. file_handle.close()
  157.  
  158. print("Phone saved successfully!")
  159.  
  160. # NEW JSON -file : cities.json
  161.  
  162. [
  163.   {
  164.     "name": "Finland",
  165.     "population": 5536146,
  166.     "capital": "Helsinki"
  167.   },
  168.   {
  169.     "name": "Sweden",
  170.     "population": 10402070,
  171.     "capital": "Stockholm"
  172.   },
  173.   {
  174.     "name": "France",
  175.     "population": 67413000,
  176.     "capital": "Paris"
  177.   }
  178. ]
  179.  
  180. # NEW FILE
  181.  
  182. import json
  183.  
  184. file_handle = open("cities.json", "r")
  185. content = file_handle.read()
  186. file_handle.close()
  187.  
  188. # convert JSON to Python data
  189. cities = json.loads(content)
  190.  
  191. # cities is is a list of dictionaries, so we can loop it:
  192. for city in cities:
  193.     print(city['name'])
  194.     print(city['capital'])
  195.     print()
  196.  
  197. # NEW JSON-file: americancities.json
  198.  
  199. [
  200.   {
  201.     "name": "Los Angeles",
  202.     "population": 3898747,
  203.     "state": "California"
  204.   },
  205.   {
  206.     "name": "Miami",
  207.     "population": 6166488,
  208.     "state": "Florida"
  209.   },
  210.   {
  211.     "name": "Denver",
  212.     "population": 715522,
  213.     "state": "Colorado"
  214.   }
  215. ]
  216.  
  217. # NEW FILE
  218.  
  219. import json
  220.  
  221. file_handle = open("americancities.json", "r")
  222. content = file_handle.read()
  223. file_handle.close()
  224.  
  225. # convert JSON to Python data
  226. cities = json.loads(content)
  227.  
  228. for city in cities:
  229.     print(city['name'])
  230.     print(city['state'])
  231.     print(city['population'])
  232.     print()
  233.  
  234. # PHASE 2: add new city into data
  235.  
  236. new_city_name = input("New city, name:\n")
  237. new_city_state = input("New city, state:\n")
  238. new_city_population = input("New city, population:\n")
  239. new_city_population = int(new_city_population)
  240.  
  241. # build a new dictionary based on data given by user
  242. new_city = {
  243.     "name": new_city_name,
  244.     "population": new_city_population,
  245.     "state": new_city_state
  246. }
  247.  
  248. cities.append(new_city)
  249.  
  250. # save the new version of the cities-list back to JSON
  251.  
  252. # for prettier JSON format, add parameter indent=4
  253. json_data = json.dumps(cities)
  254.  
  255. file_handle = open("americancities.json", "w")
  256. file_handle.write(json_data)
  257. file_handle.close()
  258.  
  259. print("New city saved!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement