-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutf-8''C2M2L1_Reading_And_Writing_Files.py
More file actions
119 lines (75 loc) · 4.18 KB
/
utf-8''C2M2L1_Reading_And_Writing_Files.py
File metadata and controls
119 lines (75 loc) · 4.18 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python
# coding: utf-8
# # Practice Notebook: Reading and Writing Files
# In this exercise, we will test your knowledge of reading and writing files by playing around with some text files.
# <br><br>
# Let's say we have a text file containing current visitors at a hotel. We'll call it, *guests.txt*. Run the following code to create the file. The file will automatically populate with each initial guest's first name on its own line.
# In[11]:
guests = open("guests.txt", "w")
initial_guests = ["Bob", "Andrea", "Manuel", "Polly", "Khalid"]
for i in initial_guests:
guests.write(i + "\n")
guests.close()
# No output is generated for the above code cell. To check the contents of the newly created *guests.txt* file, run the following code.
# In[12]:
with open("guests.txt") as guests:
for line in guests:
print(line)
# The output shows that our *guests.txt* file is correctly populated with each initial guest's first name on its own line. Cool!
# <br><br>
# Now suppose we want to update our file as guests check in and out. Fill in the missing code in the following cell to add guests to the *guests.txt* file as they check in.
# In[13]:
new_guests = ["Sam", "Danielle", "Jacob"]
with open("guests.txt", "a") as guests:
for i in new_guests:
guests.write(i + "\n")
guests.close()
# To check whether your code correctly added the new guests to the *guests.txt* file, run the following cell.
# In[14]:
with open("guests.txt") as guests:
for line in guests:
print(line)
# The current names in the *guests.txt* file should be: Bob, Andrea, Manuel, Polly, Khalid, Sam, Danielle and Jacob.
# <br><br>
# Was the *guests.txt* file correctly appended with the new guests? If not, go back and edit your code making sure to fill in the gaps appropriately so that the new guests are correctly added to the *guests.txt* file. Once the new guests are successfully added, you have filled in the missing code correctly. Great!
# <br><br>
# Now let's remove the guests that have checked out already. There are several ways to do this, however, the method we will choose for this exercise is outlined as follows:
# 1. Open the file in "read" mode.
# 2. Iterate over each line in the file and put each guest's name into a Python list.
# 3. Open the file once again in "write" mode.
# 4. Add each guest's name in the Python list to the file one by one.
#
# <br>
# Ready? Fill in the missing code in the following cell to remove the guests that have checked out already.
# In[15]:
checked_out=["Andrea", "Manuel", "Khalid"]
temp_list=[]
with open("guests.txt", "r") as guests:
for g in guests:
temp_list.append(g.strip())
with open("guests.txt", "w") as guests:
for name in temp_list:
if name not in checked_out:
guests.write(name + "\n")
# To check whether your code correctly removed the checked out guests from the *guests.txt* file, run the following cell.
# In[16]:
with open("guests.txt") as guests:
for line in guests:
print(line)
# The current names in the *guests.txt* file should be: Bob, Polly, Sam, Danielle and Jacob.
# <br><br>
# Were the names of the checked out guests correctly removed from the *guests.txt* file? If not, go back and edit your code making sure to fill in the gaps appropriately so that the checked out guests are correctly removed from the *guests.txt* file. Once the checked out guests are successfully removed, you have filled in the missing code correctly. Awesome!
# <br><br>
# Now let's check whether Bob and Andrea are still checked in. How could we do this? We'll just read through each line in the file to see if their name is in there. Run the following code to check whether Bob and Andrea are still checked in.
# In[17]:
guests_to_check = ['Bob', 'Andrea']
checked_in = []
with open("guests.txt","r") as guests:
for g in guests:
checked_in.append(g.strip())
for check in guests_to_check:
if check in checked_in:
print("{} is checked in".format(check))
else:
print("{} is not checked in".format(check))
# We can see that Bob is checked in while Andrea is not. Nice work! You've learned the basics of reading and writing files in Python!