-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0063_writing_file.py
More file actions
38 lines (26 loc) · 1.02 KB
/
0063_writing_file.py
File metadata and controls
38 lines (26 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
cities = ["Adelaide", "Alice Springs", "Darwin", "Melbourne", "Sydney"]
# writing to file
with open('./data/cities.txt', mode='w') as city_file:
for city in cities:
print(city, file=city_file)
# reading to file
cities_2 = []
with open('./data/cities.txt', mode='r') as city_file:
for city in city_file:
cities_2.append(city.strip("\n")) # strip not only removes the leading and trailing spaces, it also removes new line chars
print(cities_2)
for city in cities:
print(city, end="")
print()
imelda = "More Mayhem", "Imelda MAy", "2011", ((1, "Pulling the Rug"), (2, "Psycho"), (3, "Mayhem"), (4, "Kentish Town Waltz"))
with open("./data/imelda3.txt", mode="w") as imelda_file:
print(imelda, file=imelda_file)
print()
with open("./data/imelda3.txt", mode="r") as imelda_file:
contents = imelda_file.readline()
imelda = eval(contents) # evaluates the format of string in the file and determines its data structure
print(imelda)
title, artist, year, tracks = imelda
print(title)
print(artist)
print(year)