# Charles Edgar
# November 4, 2021
# In this lesson I learned how to read a file as a list with .readlines() and how to close a file.
# In this lesson I did not have trouble understanding the material. My biggest struggle was simply
# remembernig what files I had opened, how I had changed the files, when I would have to re-open
# the files, etc. That problem only required scrolling up and reading back what I had done to figure
# out what I would need to do to get where I wanted to be.
# [ ] Run to download file to notebook
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem1.txt -o poem1.txt
# [ ] 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
# [ ] review and run example
# Print each item in the list
for line in poem_lines:
print(line)
# [ ] import cities
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities -o cities.txt
# [ ] 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()
# [ ] use list iteration to print each city in cities_lines list
for city in cities_lines:
print(city)
# [ ] 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)
# [ ] print each list item
for line in poem_lines:
print(line)
# [ ] remove the last character of each list item, which is "\n"
count = 0
# Each line in the list becomes itself without the last character (\n)
for line in poem_lines:
poem_lines[count] = line[:-1]
count += 1
print(poem_lines)
# [ ] print each list item
for line in poem_lines:
print(line)
# [ ] 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()
print(cities_lines)
# [ ] remove the last character, "\n", of each cities_lines list item
count = 0
# Each line in the list becomes itself without the last character (\n)
for line in cities_lines:
cities_lines[count] = city[:-1]
count += 1
# [ ] print each list item in cities_lines
for line in cities_lines:
print(line)
# [ ] review and run example: open and readlines of poem1.txt
poem1 = open('poem1.txt', 'r')
# [ ] review and run example: readlines breaks if file is no longer open
poem_lines = poem1.readlines()
print(poem_lines)
# [ ] review and run example: Close poem1
poem1.close()
# [ ] open cities.txt as cities_file
cities_file = open('cities.txt','r')
# [ ] read the lines as cities_lines
cities_lines = cities_file.readlines()
# [ ] print the cities that start with the letter "D" or greater
for line in cities_lines:
if line.lower().startswith('a'):
print(line)
elif line.lower().startswith('b'):
print(line)
elif line.lower().startswith('c'):
print(line)
elif line.lower().startswith('d'):
print(line)
# [ ] test that file is closed
cities_lines =cities_file.readlines()
print(cities_lines)
# [ ] close cities_file
cities_file.close()
# [ ] 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
# [ ] open poem2.txt as poem2_text in read mode
poem2_text = open('poem2.txt','r')
# [ ] create a list of strings, called poem2_lines, from each line of poem2_text
poem2_lines = poem2_text.readlines()
# [ ] 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
# [ ] print the poem2 lines in reverse order
poem2_lines.reverse()
print(poem2_lines)