#connerkahn
#11/9/21
#I learned about files
#I had lots of difficulty with getting curl to work finally finding this notebook
# [ ] review and run example
!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
# [ ] test cities.txt was opened
cities_file = open("cities.txt")
cities_file
# [ ] review and run example
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
# [ ] display the string: cities
cities_contents = cities_file.read()
cities_contents
print(cities_contents)
# [ ] print the string: cities
# [ ] review and run example to read poem1.txt 10 characters at a time
poem_file = open('poem1.txt', 'r')
poem_10char = poem_file.read(10)
print(poem_10char)
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_text.txt
# [ ] digits of pi
# 2. open as digits_of_pi_text
# 3. read() 4 char of digits_of_pi_text to pi_digits variable
# 4. print pi_digits
pi_file = open("digits_of_pi_text.txt")
pi_contents = pi_file.read(4)
pi_contents
print(pi_contents)
# [ ] digits of pi
# 5. add to pi_digits string with string addition
# a. add next 4 characters from digits_of_pi obtained from read()
# b. run the cell multiple times to get more digits of *pi*
pi2 = pi_file.read(4)
print(pi2)
print(pi_contents + pi2)
# [ ] review and run example
poem_file = open('poem1.txt', 'r')
poem_part = poem_file.read(15).upper()
print(poem_part)
# [ ] review and run example
poem_part = poem_file.read(6).title()
print(poem_part)
# [ ] review and run example
poem_part = poem_file.read(6)
print(poem_part)
print(poem_part.isalpha(), "isalpha() because of `\\n`")
poem_part
# [ ] review and run example
poem_file = open('poem1.txt', 'r')
poem_text = poem_file.read()
print(poem_text[8:26])
# [ ] complete the task
cities_file = open("cities.txt")
cities_contents = cities_file.read()
initial = ''
for char in cities_contents:
if char.isupper():
initial += char
elif char == "\n":
initial += char
print(initial)