# Zhi Yuan "Oscar" Lin
# 5 November 2021
# In this lesson, I learned how to use the ".readline()" and ".strip()" methods through examples and functions.
# Additionally, I learned more regarding how to importing opening, and closing a file.
# There were no major problems or questions throughout this lesson.
# [ ]Run to download file poem1.txt
# This program imports a file named "poem1.txt" by using the !curl command.
!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 788 0 --:--:-- --:--:-- --:--:-- 788
# [ ] review and run example
# open address to file
# The variable "poem1" is assigned to the return file object from using the open() function to open the file named
# "poem1.txt" in read mode, and text mode by default.
poem1 = open('poem1.txt', 'r')
# [ ] review and run example
# readline 1, 2, 3
# The variable "poem_line1" is assigned to the string form of the first line of the value assigned to the poem_1 variable
# due to the file method ".readline()".
poem_line1 = poem1.readline()
# The variable "poem_line2" is assigned to the string form of the second line of the value assigned to the poem_1 variable
# due to the file method ".readline()".
poem_line2 = poem1.readline()
# The variable "poem_line3" is assigned to the string form of the third line of the value assigned to the poem_1 variable
# due to the file method ".readline()".
poem_line3 = poem1.readline()
# [ ] review and run example: print the first 3 .readline() values
# This function displays the concatenated string formed from the value assigned to the poem_line1 variable, the value
# assigned to the poem_line2 variable, and the value assigned to the poem_line3 variable, in the output.
print(poem_line1 + poem_line2 + poem_line3)
Loops I repeat
loops
loops
# [ ] review and run example printing return value & re-run several times
# This function displays the string form of the fourth line of the value assigned to the poem_1 variable, due to the file
# method ".readline()". If one re-runs several times, the function will display the remaining lines of the value assigned
# to the poem1 variable.
print(poem1.readline())
loops
# [ ] review and run example to close file in memory- then run previous cell
# This program removes the reference created by the file open() function that is assigned to the poem1 variable, by using
# the file method ".close()".
poem1.close()
# [ ] import https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow as rainbow.txt
# This program imports a file named "rainbow.txt" by using the !curl command.
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow -o rainbow.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 43 100 43 0 0 1869 0 --:--:-- --:--:-- --:--:-- 1954
# [ ] open rainbow.txt as rainbow_text
# The variable "rainbow_text" is assigned to the return file object from using the open() function to open the file named
# "rainbow.txt" in read mode and text mode by default.
rainbow_text = open('rainbow.txt')
# [ ] read the first 3 lines into variables: color1, color2, color3
# The variable "color1" is assigned to the string form of the first line of the value assigned to the rainbow_text variable
# due to the file method ".readline()".
color1 = rainbow_text.readline()
# The variable "color2" is assigned to the string form of the first line of the value assigned to the rainbow_text variable
# due to the file method ".readline()".
color2 = rainbow_text.readline()
# The variable "color3" is assigned to the string form of the first line of the value assigned to the rainbow_text variable
# due to the file method ".readline()".
color3 = rainbow_text.readline()
# [ ] close rainbow.txt
# This program removes the reference created by the file open() function that is assigned to the rainbow_text variable, by
# using the file method ".close()".
rainbow_text.close()
# [ ] print the first 3 colors
# This function displays the value assigned to the color1 variable in the output.
print(color1)
# This function displays the value assigned to the color2 variable in the output.
print(color2)
# This function displays the value assigned to the color3 variable in the output.
print(color3)
red
orange
yellow
# [ ] review and run example
# open address to file
# The variable "poem1" is assigned to the return file object from using the open() function to open the file named
# "poem1.txt" in read mode, and text mode by default.
poem1 = open('poem1.txt', 'r')
# [ ] review and run example - use a while loop to read each line of a file
# remove last character ('\n') and print as upper case
# The variable "poem_line" is assigned to the string form of the first line of the value assigned to the poem1 variable by
# using the file method ".readline()".
poem_line = poem1.readline()
# In this case, the while loop is followed by the variable "poem_line". Hence, it will evaluate true as long there there is
# a value assigned to the poem_line variable. Additionally, the codes that are indented and follow the while loop will
# be part of the loop. Thus, the loop starts with the value assigned to the poem_line variable being display in the output,
# after the last character is removed and the value being changed into all uppercases due to the string method ".upper()".
# Subsequently, the poem_line variable will be reassigned to the string form of the next line of the value assigned to the
# poem1 variable, due to the file method ".readline()". This loop will continue until the while loop evaluates false.
while poem_line:
print(poem_line[:-1].upper())
poem_line = poem1.readline()
LOOPS I REPEAT
LOOPS
LOOPS
LOOPS
I REPEAT
UNTIL I
BREAK
# [ ] review and run example
# This program removes the reference created by the file open() function that is assigned to the poem1 variable, by
# using the file method ".close()".
poem1.close()
# [ ] open rainbow.txt as rainbow_text as read-only
# The variable "rainbow_text" is assigned to the return file object from using the open() function to open the file named
# "rainbow.txt" in read mode, and text mode by default.
rainbow_text = open('rainbow.txt', 'r')
# [ ] read the color from lines of rainbow_text in a while loop
# [ ] print each color capitalized as the loop runs
# The variable "rainbow_line" is assigned to the string form of the first line of the value assigned to the rainbow_text
# variable due to the file method ".readline()".
rainbow_line = rainbow_text.readline()
# In this case, the while loop is followed by the variable "rainbow_line". Hence, it will evaluate true as long there there
# is a value assigned to the rainbow_line variable. Additionally, the codes that are indented and follow the while loop
# will be part of the loop. Thus, the loop starts with the value assigned to the rainbow_line variable being display in the
# output, after the value is capitalized due to the string method ".capitalize()". Subsequently, the rainbow_line variable
# will be reassigned to the string form of the next line of the value assigned to the rainbow_text variable, due to the file
# method ".readline()". This loop will continue until the while loop evaluates false.
while rainbow_line:
print(rainbow_line.capitalize())
rainbow_line = rainbow_text.readline()
Red
Orange
Yellow
Green
Blue
Indigo
Violet
# [ ] close rainbow_text
# This program removes the reference created by the file open() function that is assigned to the rainbow_text variable, by
# using the file method ".close()".
rainbow_text.close()
# [ ] review and run example
# open address to file
# The variable "poem1" is assigned to the return file object from using the open() function to open the file named
# "poem1.txt" in read mode, and text mode by default.
poem1 = open('poem1.txt', 'r')
# [ ] review and run example - readline while loop without removing '\n'
# The variable "poem_line" is assigned to the string form of the first line of the value assigned to the poem1 variable by
# using the file method ".readline()".
poem_line = poem1.readline()
# In this case, the while loop is followed by the variable "poem_line". Hence, it will evaluate true as long there there
# is a value assigned to the poem_line variable. Additionally, the codes that are indented and follow the while loop
# will be part of the loop. Thus, the loop starts with the value assigned to the poem_line variable being display in the
# output. Subsequently, the poem_line variable will be reassigned to the string form of the next line of the value assigned
# to the poem1 variable, due to the file method ".readline()". This loop will continue until the while loop evaluates false.
while poem_line:
print(poem_line)
poem_line = poem1.readline()
# This program removes the reference created by the file open() function that is assigned to the poem1 variable, by
# using the file method ".close()".
poem1.close()
Loops I repeat
loops
loops
loops
I repeat
until I
break
# [ ] review and run example - readline with .strip() to remove '\n'
# The variable "poem1" is assigned to the return file object from using the open() function to open the file named
# "poem1.txt" in read mode, and text mode by default.
poem1 = open('poem1.txt', 'r')
# The variable "poem_line" is assigned to the string form of the first line of the value assigned to the poem1 variable by
# using the file method ".readline()", after the leading and trailing whitespace in the value assigned to the poem1 variable
# are removed due to it being attached to the .strip() method.
poem_line = poem1.readline().strip()
# In this case, the while loop is followed by the variable "poem_line". Hence, it will evaluate true as long there there
# is a value assigned to the poem_line variable. Additionally, the codes that are indented and follow the while loop
# will be part of the loop. Thus, the loop starts with the value assigned to the poem_line variable being display in the
# output. Subsequently, the poem_line variable will be reassigned to the string form of the next line of the value assigned
# to the poem1 variable, due to the file method ".readline()", after the leading and trailing whitespace in the value
# assigned to the poem1 variable are removed due it being attrached to the .strip() method. This loop will continue until
# the while loop evaluates false.
while poem_line:
print(poem_line)
poem_line = poem1.readline().strip()
# This program removes the reference created by the file open() function that is assigned to the poem1 variable, by
# using the file method ".close()".
poem1.close()
Loops I repeat
loops
loops
loops
I repeat
until I
break
# [ ] open rainbow.txt as rainbow_text as read-only
# The variable "rainbow_text" is assigned to the return file object from using the open() function to open the file named
# "rainbow.txt" in read mode, and text mode by default.
rainbow_text = open('rainbow.txt', 'r')
# [ ] read a color from each line of rainbow_text in a while loop
# use .strip to remove the whitespace '\n' character
# print each color upper case
# The variable "rainbow_line" is assigned to the string form of the first line of the value assigned to the rainbow_text
# variable due to the file method ".readline()", after the leading and trailing whitespace in the value assigned to the
# rainbow_text are removed due to it being attached to the .strip() method.
rainbow_line = rainbow_text.readline().strip()
# In this case, the while loop is followed by the variable "rainbow_line". Hence, it will evaluate true as long there there
# is a value assigned to the rainbow_line variable. Additionally, the codes that are indented and follow the while loop
# will be part of the loop. Thus, the loop starts with the value assigned to the rainbow_line variable being display in the
# output, after the value is changed into all uppercases due to the string method ".upper()". Subsequently, the rainbow_line
# variable will be reassigned to the string form of the next line of the value assigned to the rainbow_text variable, due
# to the file method ".readline()", after the leading and trailing whitespace in the value assigned to the rainbow_text
# variable are removed due it being attrached to the .strip() method. This loop will continue until the while loop evaluates
# false.
while rainbow_line:
print(rainbow_line.upper())
rainbow_line = rainbow_text.readline().strip()
RED
ORANGE
YELLOW
GREEN
BLUE
INDIGO
VIOLET
# [ ] review and run example: import rainbow_messy.txt
# This program imports a file named "rainbow_messy.txt" by using the !curl command.
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow_messy -o rainbow_messy.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 71 100 71 0 0 7888 0 --:--:-- --:--:-- --:--:-- 7888
# [ ] review and run example: open file read only
# The variable "rainbow_messy" is assigned to the return file object from using the open() function to open the file named
# "rainbow_messy.txt" in read mode, and text mode by default.
rainbow_messy = open('rainbow_messy.txt', 'r')
# [ ] review and run example: .readline() without .strip()
# The variable "color" is assigned to the string form of the first line of the value assigned to the rainbow_messy
# variable due to the file method ".readline()".
color = rainbow_messy.readline()
# In this case, the while loop is followed by the variable "color". Hence, it will evaluate true as long there there
# is a value assigned to the color variable. Additionally, the codes that are indented and follow the while loop
# will be part of the loop. Thus, the loop starts with the value assigned to the color variable being display in the
# output. Subsequently, the color variable will be reassigned to the string form of the next line of the value assigned
# to the rainbow_messy variable, due to the file method ".readline()". This loop will continue until the while loop
# evaluates false.
while color:
print(color)
color = rainbow_messy.readline()
**red**
**orange**
**yellow**
**green**
**blue**
**indigo**
**violet**
# [ ] review and run example: strip "*" and newline ('\n')
# The variable "rainbow_messy" is assigned to the return file object from using the open() function to open the file named
# "rainbow_messy.txt" in read mode, and text mode by default.
rainbow_messy = open('rainbow_messy.txt', 'r')
# The variable "color" is assigned to the string form of the first line of the value assigned to the rainbow_text
# variable due to the file method ".readline()", after the leading and trailing whitespace and the character "*" in the
# value assigned to the color variable are removed due to it being attached to the .strip() method.
color = rainbow_messy.readline().strip('*\n')
# In this case, the while loop is followed by the variable "color". Hence, it will evaluate true as long there there
# is a value assigned to the color variable. Additionally, the codes that are indented and follow the while loop
# will be part of the loop. Thus, the loop starts with the value assigned to the color variable being display in the
# output. Subsequently, the color variable will be reassigned to the string form of the next line of the value assigned
# to the rainbow_messy variable, due to the file method ".readline()", after the leading and trailing whitespace and the
# character "*" in the value assigned to the color variable are removed due to it being attached to the .strip() method.
# This loop will continue until the while loop evaluates false.
while color:
print(color)
color = rainbow_messy.readline().strip('*\n')
# This program removes the reference created by the file open() function that is assigned to the rainbow_messy variable, by
# using the file method ".close()".
rainbow_messy.close()
red
orange
yellow
green
blue
indigo
violet
# [ ] import the file
# This program imports a file named "cities_messy.txt" by using the !curl command.
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities_messy -o cities_messy.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 77 100 77 0 0 5133 0 --:--:-- --:--:-- --:--:-- 5133
# [ ] run to read the file into memory
# The variable "cities_messy" is assigned to the return file object from using the open() function to open the file named
# "cities_messy.txt" in read mode, and text mode by default.
cities_messy = open('cities_messy.txt', 'r')
# [ ] edit the code to remove leading or trailing colon, newline and space characters
# The variable "line" is assigned to the string form of the first line of the value assigned to the cities_messy
# variable due to the file method ".readline()", after the leading and trailing whitespace and the character ":" in the
# value assigned to the color variable are removed due to it being attached to the .strip() method.
line = cities_messy.readline().strip(':\n')
# In this case, the while loop is followed by the variable "line". Hence, it will evaluate true as long there there
# is a value assigned to the color variable. Additionally, the codes that are indented and follow the while loop
# will be part of the loop. Thus, the loop starts with the value assigned to the line variable being display in the
# output. Subsequently, the line variable will be reassigned to the string form of the next line of the value assigned
# to the cities_messy variable, due to the file method ".readline()", after the leading and trailing whitespace and the
# character ":" in the value assigned to the color variable are removed due to it being attached to the .strip() method.
# This loop will continue until the while loop evaluates false.
while line:
print(line)
line = cities_messy.readline().strip(':\n')
# This program removes the reference created by the file open() function that is assigned to the cities_messy variable, by
# using the file method ".close()".
cities_messy.close()
Beijing
Cairo
London
Nairobi
New York City
Sydney
Tokyo
# [ ] import https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem2_messy as poem2_messy.txt
# This program imports a file named "poem2_messy.txt" by using the !curl command.
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem2_messy -o poem2_messy.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 75 100 75 0 0 1086 0 --:--:-- --:--:-- --:--:-- 1086
# [ ] open poem2_messy.txt as poem2_messy in read mode
# The variable "poem2_messy" is assigned to the return file object from using the open() function to open the file named
# "poem2_messy.txt" in read mode, and text mode by default.
poem2_messy = open('poem2_messy.txt', 'r')
# [ ] edit while loop to strip the leading and trailing parenthesis, and newlines
# [ ] print the poem
# The variable "line" is assigned to the string form of the first line of the value assigned to the poem2_messy
# variable due to the file method ".readline()", after the leading and trailing whitespace and the characters "(" and ")"
# in the value assigned to the poem2_messy variable are removed due to it being attached to the .strip() method.
line = poem2_messy.readline().strip("()\n")
# In this case, the while loop is followed by the variable "line". Hence, it will evaluate true as long there there
# is a value assigned to the color variable. Additionally, the codes that are indented and follow the while loop
# will be part of the loop. Thus, the loop starts with the value assigned to the line variable being display in the
# output. Subsequently, the line variable will be reassigned to the string form of the next line of the value assigned
# to the poem2_messy variable, due to the file method ".readline()", after the leading and trailing whitespace and the
# characters "(" and ")" in the value assigned to the color variable are removed due to it being attached to the .strip()
# method. This loop will continue until the while loop evaluates false.
while line:
print(line)
line = poem2_messy.readline().strip("()\n")
# This program removes the reference created by the file open() function that is assigned to the poem2_messy variable, by
# using the file method ".close()".
poem2_messy.close()
while True
I loop
True
loop
True
loop
not True
False
end