# Noemi Cabrera
# 9 November 2021
# In this lesson, I learned to use .readlines() to read text from files as a list of lines
# and use .close() to close a file, which helps to free system resources. Along with these
# 2 methods I learned, I used other methods that I learned in previous lessons to perform
# the tasks.
# I didn't have any difficulties in this lesson.
# [ In this code, a file is imported into this notebook using the curl statement. The name
# poem1.txt is given to the file. ] Run to download file to notebook
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem1.txt -o poem1.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 56 100 56 0 0 608 0 --:--:-- --:--:-- --:--:-- 608
# [ In this code, the imported poem1.txt file is opened in read mode by using open(). Then,
# this file is read as a list using the readlines() method and stored in the poem_lines
# variable. The list is displayed and we can see the strings include the formatting characters
# the file had when imported. ] review and run example
# open address to file
poem1 = open('poem1.txt', 'r')
# readlines and print as a list
poem_lines = poem1.readlines()
poem_lines
# [ In this code, the for..in loop is used to iterate through each item/string in the poem_lines
# list and print each one. The formatting method '\n', which the strings contained, is
# applied in the output when printing the strings. ] review and run example
for line in poem_lines:
print(line)
Loops I repeat
loops
loops
loops
I repeat
until I
break
# [ In this code, a file is imported using a curl statement and named cities.txt.] import cities
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities -o cities.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 56 100 56 0 0 788 0 --:--:-- --:--:-- --:--:-- 788
# [ In this code, the above imported file is opened in read mode using print() and stored
# in the cities_file variable. Then, this file is read as a list using the readlines() method
# and stored in the cities_lines variable. ] open cities.txt as cities_file and read the file
# as a list: cities_lines
cities_file = open("cities.txt", "r")
cities_lines = cities_file.readlines()
# [ In this code, the for..in loop iterates through each item in the cities_lines list. Then,
# each string is printed along with the '\n' formatting in the output. ]
# use list iteration to print each city in cities_lines list
for line in cities_lines:
print(line)
Beijing
Cairo
London
Nairobi
New York City
Sydney
Tokyo
# In this code, the poem1.txt is re-opened in read mode using the open() method. Then,
# this file is read as a list using the readlines() method and stored in the poem_lines
# variable. The list is printed.
# [ ] review and run examples
# [ ] re-open file and read file as a list of strings
poem1 = open('poem1.txt', 'r')
poem_lines = poem1.readlines()
print(poem_lines)
['Loops I repeat\n', 'loops\n', 'loops\n', 'loops\n', 'I repeat\n', 'until I\n', 'break\n']
# [ In this code, the for..in loop is used to iterate through each item/string in the poem_lines
# list and print each one. The formatting method '\n', which the strings contained, is
# applied when printing the strings. ] print each list item
for line in poem_lines:
print(line)
Loops I repeat
loops
loops
loops
I repeat
until I
break
# [ In this code, the for..in loop is used to remove the last character of each item in the poem_lines
# list, which is \n. The variable count is initialized and added 1 each time until there aren't more
# items to iterate through. The final list is printed. ]
# remove the last character of each list item, which is "\n"
count = 0
for line in poem_lines:
poem_lines[count] = line[:-1]
count += 1
print(poem_lines)
['Loops I repeat', 'loops', 'loops', 'loops', 'I repeat', 'until I', 'break']
# [ In this code, the for..in loop is used to iterate through each line of the previously
# modified poem_lines lis. Then, each line is printed without an added extra new line.]
# print each list item
for line in poem_lines:
print(line)
Loops I repeat
loops
loops
loops
I repeat
until I
break
# In this code, the above imported file is opened in read mode using print() and stored
# in the cities_file variable. Then, this file is read as a list using the readlines() method
# and stored in the cities_lines variable.
# [ ] re-open file and read file as a list of strings
# [ ] open cities.txt as cities_file and read the file as a list: cities_lines
cities_file = open("cities.txt", "r")
cities_lines = cities_file.readlines()
# [ In this code, the for..in loop is used to remove the last character of each item in the cities_lines
# list, which is \n. The variable 'iterate' is initialized and added 1 each time until there aren't more
# items left to iterate through. The final list is printed. ]
# remove the last character, "\n", of each cities_lines list item
iterate = 0
for line in cities_lines:
cities_lines[iterate] = line[:-1]
iterate += 1
# [ In this code, each item/string of the cities_lines list is printed using a for..in loop.
# Now, there are no extra new lines in the output since the '\n' formatting characters were
# deleted in the previous code cell. ] print each list item in cities_lines
for line in cities_lines:
print(line)
Beijing
Cairo
London
Nairobi
New York City
Sydney
Tokyo
# [ In this code, the poem1.txt is opened in read mode using the open() method and then stored
# in the poem1 variable. ] review and run example: open and readlines of poem1.txt
poem1 = open('poem1.txt', 'r')
# [ In this code, the poem1 file opened above is read as a list using the readlines() method
# and stored in the poem_lines variable. The list is printed. ]
# review and run example: readlines breaks if file is no longer open
poem_lines = poem1.readlines()
print(poem_lines)
['Loops I repeat\n', 'loops\n', 'loops\n', 'loops\n', 'I repeat\n', 'until I\n', 'break\n']
# [In this code, the poem1 file is closed using the .close() method. ] review and run example: Close poem1
poem1.close()
# [ In this code, the imported poem1.txt file is opened in read mode by using open(). ]
# open cities.txt as cities_file
cities_file = open('cities.txt','r' )
# [ Using the readlines() method, the previous file is read as a list using the readlines()
# method and stored in the poem_lines variable. The list is displayed.]
# read the lines as cities_lines
cities_lines = cities_file.readlines()
# [ In this code, a for..in loop is used to iterate through each item in the cities_lines
# list. Then, if the line starts with D or greater, the line is printed. If the line is
# less than D, then that line is passed. ]
# print the cities that start with the letter "D" or greater
for line in cities_lines:
if line[0] >= "D":
print(line)
else:
pass
London
Nairobi
New York City
Sydney
Tokyo
# [ To see if the file is closed or not, I used .readlines() to read the file line by line.
# If the file is closed, readlines will break. ] test that file is closed
cities_lines =cities_file.readlines()
print(cities_lines)
<_io.TextIOWrapper name='cities.txt' mode='r' encoding='UTF-8'>
# [ The cities_file is closed using the .close() method. ] close cities_file
cities_file.close()
# In this code, a file is imported into this notebook using the curl statement. The name
# poem2.txt is given to the file.
# [ ] import https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem2.txt as poem2.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem2.txt -o poem2.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 57 100 57 0 0 422 0 --:--:-- --:--:-- --:--:-- 422
# [ In this code, the poem2.txt is opened in read mode using the open() method. ]
# open poem2.txt as poem2_text in read mode
poem2_text = open("poem2.txt", "r")
# [ Using the readlines() method, the previous file is read as a list using the readlines()
# method and stored in the poem2_lines variable. ]
# create a list of strings, called poem2_lines, from each line of poem2_text
poem2_lines = poem2_text.readlines()
# [ In this code, the for..in loop is used to remove the last character of each item in the poem2_lines
# list, which is \n. The variable 'count' is initialized and added 1 each time until there aren't more
# items left to iterate through. The final list is printed. ] remove the newline character for each list item in poem2_lines
count = 0
for line in poem2_lines:
poem2_lines[count] = line[:-1]
count += 1
poem2_lines
# [ In this code, a for...in loop is used to iterate through each line in poem2_lines list.
# Then, the list is reversed using reverse() and each line is printed in reverse order. ]
# print the poem2 lines in reverse order
for line in poem2_lines:
poem2_lines.reverse()
print(line)
while True
False
True
loop
True
loop
not True
I loop
end