# Charles Edgar
# November 4, 2021
# In this lesson I learned how to open and read files in code.
# I did not have much difficulty in this lesson. The !curl command and some of the outputs seemed
# a little confusing at first, but once I broke them down and looked at them piece by piece, it
# became much easier to understand.
# [ ] review and run example
# Import poem1.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem1.txt -o poem1.txt
# [ ]Run to open the file in memory as poem_file
poem_file = open('poem1.txt', 'r')
# [ ] run and review code to test if open worked
# should display name='poem1.txt' and no errors
poem_file
# [ ] import cities.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities -o cities.txt
# [ ] open cities.txt as cities_file
cities_file = open('cities.txt', 'r')
# [ ] test cities.txt was opened
print(cities_file)
# [ ] review and run example
# Assigns the content of poem_file to a variable
poem_contents = poem_file.read()
# [ ] review and run example
# shows the file as a string with formatting characters such as "\n", output should be non-blank
poem_contents
# [ ] review and run example
# since .read() loaded the file as a string it can be printed
print(poem_contents)
# [ ] after import and open of cities.txt in task 1
# [ ] read cities_file as cities
cities = cities_file.read()
# [ ] display the string: cities
cities
# [ ] print the string: cities
print(cities)
# [ ] review and run example to read poem1.txt 10 characters at a time
# Opens poem1.txt
poem_file = open('poem1.txt', 'r')
# Reads the first 10 characters
poem_10char = poem_file.read(10)
# Prints the first 10 characters
print(poem_10char)
# Displays first 10 characters
poem_10char
# [ ] review and run example, + 10 more characters
# reads and displays without storing in a variable
poem_file.read(10)
# [ ] review and run example, + 10 more characters
# reads and stores in variable poem_parts
poem_parts = poem_file.read(10)
print(poem_parts)
# [ ] REPEATEDLY RUN this cell, + 5 more characters each time run are appended using string addition
# [ ] consider why no additional text displays after multiple runs
poem_parts += poem_file.read(5)
print(poem_parts)
# [ ] digits of pi
# 1. import digits_of_pi.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/digits_of_pi -o digits_of_pi.txt
# [ ] digits of pi
# 2. open as digits_of_pi_text
digits_of_pi_text = open('digits_of_pi.txt', 'r')
# 3. read() 4 char of digits_of_pi_text to pi_digits variable
pi_digits = digits_of_pi_text.read(4)
# 4. print pi_digits
print(pi_digits)
# [ ] digits of pi
# 5. add to pi_digits string with string addition
# a. add next 4 characters from digits_of_pi obtained from read()
pi_digits += digits_of_pi_text.read(4)
# b. run the cell multiple times to get more digits of *pi*
print(pi_digits)
# [ ] review and run example
# Opens poem1.txt
poem_file = open('poem1.txt', 'r')
# Reads the first 15 characters as uppercased into variable poem_part
poem_part = poem_file.read(15).upper()
# Print poem_part
print(poem_part)
# [ ] review and run example
# Reads the next 6 characters in title format into variable
poem_part = poem_file.read(6).title()
# Print poem_part
print(poem_part)
# [ ] review and run example
# Reads the next 6 characters into variable
poem_part = poem_file.read(6)
# Print poem_part
print(poem_part)
# Prints message explaining why poem_part is not all alphabetical
print(poem_part.isalpha(), "isalpha() because of `\\n`")
# Display poem_part
poem_part
# [ ] review and run example
# Opens poem1.txt
poem_file = open('poem1.txt', 'r')
# Reads file into variable
poem_text = poem_file.read()
# Prints the poem from the 8th to the 26th character
print(poem_text[8:26])
# [ ] complete the task
# Opens cities.txt and reads it into a variable
cities_file = open('cities.txt', 'r')
cities = cities_file.read()
# Defines empty string
initials = ''
# Appends uppercase and newline characters in cities to initials
for city in cities:
if city.isupper():
initials += city
elif city == '\n':
initials += city
# Prints initials of each city
print(initials)