#Conner Kahn
#11/11
#I learned more about files specifically reading lines and closing them
# [ ] 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 736 0 --:--:-- --:--:-- --:--:-- 736
# [ ] 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
for line in poem_lines:
print(line)
Loops I repeat
loops
loops
loops
I repeat
until I
break
# [ ] import cities
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities -o cities_file
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 56 100 56 0 0 691 0 --:--:-- --:--:-- --:--:-- 700
# [ ] open cities.txt as cities_file and read the file as a list: cities_lines
cities = open("cities_file", "r")
citieslines = cities.readlines()
citieslines
# [ ] use list iteration to print each city in cities_lines list
for line in citieslines:
print(line)
Beij
Ca
Lon
Nair
New York C
Syd
To
# [ ] 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']
# [ ] print each list item
for line in poem_lines:
print(line)
Loops I repeat
loops
loops
loops
I repeat
until I
break
# [ ] 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']
# [ ] print each list item
for line in poem_lines:
print(line)
Loops I repeat
loops
loops
loops
I repeat
until I
break
# [ ] 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 = open("cities_file", "r")
citieslines = cities.readlines()
citieslines
# [ ] remove the last character, "\n", of each cities_lines list item
count = 0
for line in citieslines:
citieslines[count] = line[:-1]
count += 1
print(citieslines)
['Beijing', 'Cairo', 'London', 'Nairobi', 'New York City', 'Sydney', 'Tokyo']
# [ ] print each list item in cities_lines
for line in citieslines:
print(line)
Beijing
Cairo
London
Nairobi
New York City
Sydney
Tokyo
# [ ] 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)
['Loops I repeat\n', 'loops\n', 'loops\n', 'loops\n', 'I repeat\n', 'until I\n', 'break\n']
# [ ] review and run example: Close poem1
poem1.close()
# [ ] open cities.txt as cities_file
cities = open("cities_file", "r")
# [ ] read the lines as cities_lines
citieslines = cities.readlines()
# [ ] print the cities that start with the letter "D" or greater
for city in cities:
if city.startswith('D'):
print(city)
# [ ] test that file is closed
if cities.close():
print("closed")
else:
print('Open')
Open
# [ ] close cities_file
cities.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
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 57 100 57 0 0 721 0 --:--:-- --:--:-- --:--:-- 721
# [ ] open poem2.txt as poem2_text in read mode
poem2text = open('poem2.txt', 'r')
poem2text
# [ ] create a list of strings, called poem2_lines, from each line of poem2_text
poem2lines = poem2text.readlines()
print(poem2lines)
['while True\n', 'I loop\n', 'True\n', 'loop\n', 'True\n', 'loop\n', 'not True\n', 'False\n', 'end\n']
# [ ] remove the newline character for each list item in poem2_lines
count = 0
for line in poem2lines:
poem2lines[count] = line[:-1]
count += 1
print(poem2lines)
['Loops I repeat\n', 'loops\n', 'loops\n', 'loops\n', 'I repeat\n', 'until I\n', 'break\n']
['while True', 'I loop', 'True', 'loop', 'True', 'loop', 'not True', 'False', 'end']
# [ ] print the poem2 lines in reverse order
print(poem2lines[::-1])
['end', 'False', 'not True', 'loop', 'True', 'loop', 'True', 'I loop', 'while True']