Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # lecture 30.11.2022, file management in Python
- print("Welcome!")
- # NEW TEXT FILE - weekdays.txt
- Monday
- Tuesday
- Wednesday
- Thursday
- Friday
- Saturday
- Sunday
- # NEW FILE
- # open a connection to our file
- file_handle = open("weekdays.txt", "r")
- # read all contents to a variable and print
- content = file_handle.read()
- print(content)
- # always remember to close the connection in the end
- # to avoid strange file locking bugs
- file_handle.close()
- # NEW FILE
- # open a connection to our file
- file_handle = open("weekdays.txt", "r")
- counter = 1
- while True:
- # keep on getting a single line from file
- # as the are lines still left
- line = file_handle.readline()
- print(f"{counter}. {line}")
- counter = counter + 1
- # no more lines left in file => break
- if not line:
- break
- # always remember to close the connection in the end
- # to avoid strange file locking bugs
- file_handle.close()
- # NEW FILE
- # open a connection to our file
- file_handle = open("weekdays.txt", "r")
- # read all contents to a variable and print
- content = file_handle.read()
- lines = content.split("\n")
- # because lines is just a Python list, we can use loops
- for line in lines:
- print(line)
- print()
- # or we can do a numbered just like before with products
- amount = len(lines)
- for index in range(amount):
- line = lines[index]
- print(f"{index + 1}. {line}")
- # always remember to close the connection in the end
- # to avoid strange file locking bugs
- file_handle.close()
- # NEW FILE
- # open file for writing
- file_handle = open("memo.txt", "w", encoding="utf-8")
- # ask user for text, write it to file
- text = input("Give some text:\n")
- file_handle.write(text)
- # always remember to close the connection in the end
- # to avoid strange file locking bugs
- file_handle.close()
- # NEW FILE
- # open file for writing, use append instead of "w"
- file_handle = open("memo.txt", "a", encoding="utf-8")
- # ask user for text, write it to file
- text = input("Give some text:\n")
- # remember add new lines if you don't want
- # to append text on the same line
- file_handle.write(text + "\n\n")
- # always remember to close the connection in the end
- # to avoid strange file locking bugs
- file_handle.close()
- # NEW JSON FILE -> app_data.json
- {
- "name": "Rovaniemi",
- "population": 62933,
- "county": "Lapland"
- }
- # NEW FILE
- import json
- import var_dump as vd
- # get the JSON data from the file
- file_handle = open("app_data.json", "r")
- content = file_handle.read()
- file_handle.close()
- # convert JSON data (which is text) into Python data
- city = json.loads(content)
- # city is just a Python dictionary
- print(city['name'])
- print(city['county'])
- # compare the vardumps
- # vd.var_dump(content)
- # print()
- # vd.var_dump(city)
- # NEW FILE
- import json
- # create a dictionary, for example, a phone
- phone = {
- "name": "Nokia 3310",
- "release_year": 2000,
- "battery": "1000mAh",
- "camera": False,
- "weight": 133
- }
- # if you want to save JSON in prettier format
- # try adding parameter indent = 4
- content = json.dumps(phone)
- # open file for writing, write the JSON text
- # inside the file
- file_handle = open("phone.json", "w")
- file_handle.write(content)
- file_handle.close()
- print("Phone saved successfully!")
- # NEW JSON -file : cities.json
- [
- {
- "name": "Finland",
- "population": 5536146,
- "capital": "Helsinki"
- },
- {
- "name": "Sweden",
- "population": 10402070,
- "capital": "Stockholm"
- },
- {
- "name": "France",
- "population": 67413000,
- "capital": "Paris"
- }
- ]
- # NEW FILE
- import json
- file_handle = open("cities.json", "r")
- content = file_handle.read()
- file_handle.close()
- # convert JSON to Python data
- cities = json.loads(content)
- # cities is is a list of dictionaries, so we can loop it:
- for city in cities:
- print(city['name'])
- print(city['capital'])
- print()
- # NEW JSON-file: americancities.json
- [
- {
- "name": "Los Angeles",
- "population": 3898747,
- "state": "California"
- },
- {
- "name": "Miami",
- "population": 6166488,
- "state": "Florida"
- },
- {
- "name": "Denver",
- "population": 715522,
- "state": "Colorado"
- }
- ]
- # NEW FILE
- import json
- file_handle = open("americancities.json", "r")
- content = file_handle.read()
- file_handle.close()
- # convert JSON to Python data
- cities = json.loads(content)
- for city in cities:
- print(city['name'])
- print(city['state'])
- print(city['population'])
- print()
- # PHASE 2: add new city into data
- new_city_name = input("New city, name:\n")
- new_city_state = input("New city, state:\n")
- new_city_population = input("New city, population:\n")
- new_city_population = int(new_city_population)
- # build a new dictionary based on data given by user
- new_city = {
- "name": new_city_name,
- "population": new_city_population,
- "state": new_city_state
- }
- cities.append(new_city)
- # save the new version of the cities-list back to JSON
- # for prettier JSON format, add parameter indent=4
- json_data = json.dumps(cities)
- file_handle = open("americancities.json", "w")
- file_handle.write(json_data)
- file_handle.close()
- print("New city saved!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement