# Zhi Yuan "Oscar" Lin
# 8 November 2021
# In this lesson, I learned how to use the file methods, ".write()" and ".seek()" through examples and tasks. Additionally,
# I learned when to use the a+, w+, and r+ modes. As for the rest, it was review which included the following: importing,
# opening, and closing a file, using the ".strip()" method, and using file method ".write()".
# There were no major problems or questions throughout this lesson.
# [ ] review and run example
# - creates a new local file or overwrites the local file (makes it blank)
# The variable "new_file" is assigned to the return file object from using the open() function to open the file named
# "new_file.txt" in write mode, and text mode by default.
new_file = open('new_file.txt', 'w')
# [ ] review and run example to write some text to the file
# This line of code writes the text "This is line #1 with 'w'\nThis is line #2 with 'w'\nThis is line #3 withn 'w'!\n" to
# the new_file file, by using the file method ".write()".
new_file.write("This is line #1 with 'w'\nThis is line #2 with 'w'\nThis is line #3 withn 'w'!\n")
# [ ] review and run example
# - close file and re-open in read mode
# - head pointer is at start of file
# This line of code removes the reference created by the file open() function that is assigned to the new_file variable, by
# using the file method ".close()".
new_file.close()
# The variable "new_file" is assigned to the return file object from using the open() function to open the file named
# "new_file.txt" in read mode, and text mode by default.
new_file = open('new_file.txt', 'r')
# [ ] review and run example to see what was written to the file
# The variable "new_text" is assigned to the given value when the file "new_file" is attached to the file method ".read()".
new_text = new_file.read()
# This function displays the value assigned to the new_text variable in the output.
print(new_text)
# This line of code removes the reference created by the file open() function that is assigned to the new_file variable, by
# using the file method ".close()".
new_file.close()
# [ ] open planets.txt in write mode
# The variable "inner_planets_file" is assigned to the return file object from using the open() function to open the file
# named "inner_planets.txt" in write mode, and text mode by default.
inner_planets_file = open('inner_planets.txt', 'w')
# [ ] write Mercury, Venus, Earth, Mars on separate lines
# This line of code writes the text "Mercury\nVenus\nEarth\nMars\n" to the inner_planets_file file, by using the file method
# ".write()".
inner_planets_file.write('Mercury\nVenus\nEarth\nMars\n')
# [ ] close the file and re-open in read mode
# This line of code removes the reference created by the file open() function that is assigned to the inner_planets_file
# variable, by using the file method ".close()".
inner_planets_file.close()
# The variable "inner_planets_file" is assigned to the return file object from using the open() function to open the file
# named "inner_planets.txt" in read mode, and text mode by default.
inner_planets_file = open('planets.txt', 'r')
# [ ] use .read() to read the entire file contents
# The variable "inner_planets_text" is assigned to the given value when the file "planets_file" is attached to the file
# method ".read()".
inner_planets_text = inner_planets_file.read()
# [ ] print the entire file contents and close the file
# This function displays the value assigned to the inner_planets_text variable in the output.
print(inner_planets_text)
# This line of code removes the reference created by the file open() function that is assigned to the inner_planets_file
# variable, by using the file method ".close()".
inner_planets_file.close()
# [ ] review and run example
# creates a new local file or overwrites the local file (makes it blank)
# The variable "new_file" is assigned to the return file object from using the open() function to open the file named
# "new_file.txt" in write and read mode (overwrites), and text mode by default.
new_file = open('new_file.txt', 'w+')
# [ ] review and run example to see what was written to the file
# - 'w+' overwrites, we can read, but the file is ***BLANK***
# The variable "new_text" is assigned to the given value when the file "new_file" is attached to the file method ".read()".
new_text = new_file.read()
# This function displays the value assigned to the new_text variable in the output.
print(new_text)
# [ ] review and run - write to the blank file
# This line of code writes the text "This is line #1 with 'w+'\nThis is line #2 with 'w+'\n" to the new_file file, by using
# the file method ".write()".
new_file.write("This is line #1 with 'w+'\nThis is line #2 with 'w+'\n")
# [ ] review and run example - read and print (Note: the pointer is at the end of the file - result = empty string)
# The variable "new_text" is assigned to the given value when the file "new_file" is attached to the file method ".read()".
new_text = new_file.read()
# This function displays the value assigned to the new_text variable in the output.
print(new_text)
# [ ] review and run example - sets pointer to beginning of file
# This line of code sets the pointer to the beginning of the file assigned to the new_file variable due to the file method
# ".seek()".
new_file.seek(0)
# [ ] review and run example - now read starts from beginning of file
# The variable "new_text" is assigned to the given value when the file "new_file" is attached to the file method ".read()".
new_text = new_file.read()
# This function displays the value assigned to the new_text variable in the output.
print(new_text)
# # [ ] review and run example - clean up and close file
# This line of code removes the reference created by the file open() function that is assigned to the new_file variable, by
# using the file method ".close()".
new_file.close()
# [ ] open outer_planets.txt in write mode 'w+'
# The variable "outer_planets_file" is assigned to the return file object from using the open() function to open the file
# named "outer_planets.txt" in write and read mode (overwrites), and text mode by default.
outer_planets_file = open('outer_planets.txt', 'w+')
# [ ] write four outer planets in earth's solar system (Jupiter, Saturn, Uranus, Neptune) on separate lines
# This line of code writes the text "Jupiter\nSaturn\nUranus\nNeptune\n" to the outer_planets_file file, by using the file
# method ".write()".
outer_planets_file.write("Jupiter\nSaturn\nUranus\nNeptune\n")
# [ ] use .seek() to move the pointer to the start of the file
# [ ] use .read() to read the entire file contents
# This line of code sets the pointer to the beginning of the file assigned to the outer_planets_file variable due to the
# file method ".seek()".
outer_planets_file.seek(0)
# The variable "outer_planets_text" is assigned to the given value when the file "outer_planets_file" is attached to the
# file method ".read()".
outer_planets_text = outer_planets_file.read()
# [ ] print the entire file contents and close the file
# This function displays the value assigned to the outer_planets_text in the output.
print(outer_planets_text)
# This line of code removes the reference created by the file open() function that is assigned to the outer_planets_file
# variable, by using the file method ".close()".
outer_planets_file.close()
# [ ] review and run - create, write, read and print a file
# The variable "tips_file" is assigned to the return file object from using the open() function to open the file named
# "code_tips.txt" in write and read mode (overwrites), and text mode by default.
tips_file = open('code_tips.txt', 'w+')
# This line of code writes the text "-use simple function and variable names\n-comment code\n-organize code into functions\n"
# to the tips_file file, by using the file method ".write()".
tips_file.write('-use simple function and variable names\n-comment code\n-organize code into functions\n')
# This line of code sets the pointer to the beginning of the file assigned to the tips_file variable due to the file method
# ".seek()".
tips_file.seek(0)
# The variable "tips_text" is assigned to the given value when the file "tips_file" is attached to the file method ".read()".
tips_text = tips_file.read()
# This function displays the value assigned to the tips_text variable.
print(tips_text)
# [ ] review and run example - setting a specific seek() index
# This line of code sets the pointer to the start of the 14th character of the file assigned to the tips_file variable due
# to the file method ".seek()".
tips_file.seek(13)
# now read starts from 14th character of file
# The variable "tips_text" is assigned to the given value when the file "tips_file" is attached to the file method ".read()".
tips_text = tips_file.read()
# This function displays the value assigned to the tips_text variable.
print(tips_text)
# [ ] review and run example - string slicing on a read of an entire file
# read from the start
# This line of code sets the pointer to the beginning of the file assigned to the tips_file variable due to the file method
# ".seek()".
tips_file.seek(0)
# The variable "tips_text" is assigned to the given value when the file "tips_file" is attached to the file method ".read()".
tips_text = tips_file.read()
# slice from the 14th character to end
# This function only displays the 14th to the last character of the value assigned to the tips_text variable in the output.
print(tips_text[13:])
# [ ] review and run example - setting pointer to end of file with whence value = 2
# This line of code sets the pointer to the end of the file assigned to the tips_file variable due to the file method
# ".seek()".
tips_file.seek(0,2)
# This line of code adds the text "-use seek(0,2) to set read/write at end of file\n" to the tips_file file, by using the
# file method ".write()".
tips_file.write("-use seek(0,2) to set read/write at end of file\n")
# read from beginning of file - .seek(0,0) is same as .seek(0)
# This line of code sets the pointer to the beginning of the file assigned to the tips_file variable due to the file method
# ".seek()".
tips_file.seek(0,0)
# The variable "tips_text" is assigned to the given value when the file "tips_file" is attached to the file method ".read()".
tips_text = tips_file.read()
# This function displays the value assigned to the tips_text variable in the output.
print(tips_text)
# [ ] review and run example - point to file beginning and overwrite 1st line
# This line of code sets the pointer to the beginning of the file assigned to the tips_file variable due to the file method
# ".seek()".
tips_file.seek(0)
# This line of code replaces the 1st line of the tips_file variable with the text "'-use simple function and variable
#names\n', by using the file method ".write()", after it is changed into all uppercases due to the string method ".upper()".
tips_file.write('-use simple function and variable names\n'.upper())
# [ ] review and run example - show new file contents
# This line of code sets the pointer to the beginning of the file assigned to the tips_file variable due to the file method
# ".seek()".
tips_file.seek(0,0)
# The variable "tips_text" is assigned to the given value when the file "tips_file" is attached to the file method ".read()".
tips_text = tips_file.read()
# This function displays the value assigned to the tips_text variable in the output.
print(tips_text)
# [ ] open a new file days.txt in write plus read mode 'w+'
# [ ] write week days (Monday - Friday) on separate lines to the file
# The variable "days_file" is assigned to the return file object from using the open() function to open the file named
# "days.txt" in write and read mode (overwrites), and text mode by default.
days_file = open('days.txt', "w+")
# This line of code writes the text "Monday\nTuesday\nWednesday\nThursday\nFriday\n" to the days_file file, by using the
# file method ".write()".
days_file.write("Monday\nTuesday\nWednesday\nThursday\nFriday\n")
# [ ] use .seek() to move the pointer to the start of the file
# [ ] use .read() to read the entire file contents
# [ ] print the entire file contents and close the file
# This line of code sets the pointer to the beginning of the file assigned to the tips_file variable due to the file method
# ".seek()".
days_file.seek(0)
# The variable "days_text" is assigned to the given value when the file "days_file" is attached to the file method ".read()".
days_text = days_file.read()
# This function displays the value assigned to the days_text variable in the output.
print(days_text)
# [ ] use .seek() to move the pointer to the end of the file
# [ ] write the weekend days (Saturday & Sunday)
# This line of code sets the pointer to the end of the file assigned to the days_file variable due to the file method
# ".seek()".
days_file.seek(0,2)
# This line of code adds the text "Saturday\nSunday\n" to the tips_file file, by using the file method ".write()".
days_file.write("Saturday\nSunday\n")
# [ ] use .seek() to move the pointer to the start of the file
# [ ] use .read() to read the entire file contents
# [ ] print the entire file contents and close the file
# This line of code sets the pointer to the beginning of the file assigned to the days_file variable due to the file method
# ".seek()".
days_file.seek(0)
# The variable "days_text" is assigned to the given value when the file "days_file" is attached to the file method ".read()".
days_text = days_file.read()
# This function displays the value assigned to the days_text variable in the output.
print(days_text)
# [ ] review and run example - function writes to the open log argument
# loads funtion into memory but the funtion is not called
# This program defines a function named "logger", that contains the parameter "log", with the variable "log_entry" that will
# be assigned to the entry the user inputs when presented with the string "enter log item (enter to quit): ". Subsequently,
# the count variable is assigned to the integer "0". Additionally, there is a while loop. In this case, the while loop is
# followed by the variable "log_entry". Hence, it will evaluate true as long there there is a value assigned to the
# log_entry variable. Additionally, the codes that are indented and follow the while loop will be part of the loop. Thus,
# the loop starts with the count variable being reassigned to the value assigned to the count variable incremented by the
# integer "1". Subsequently, the text, formed from the value assigned to the count varibale, after it is changed into a
# string due to the str() function, the string ": ", the value assigned to the log_entry variable, and the escape character
# "\n", to the log file, by using the file method ".write()". Following that, the log_entry variable will be reassigned to
# the entry the user inputs when presented with the string "enter log item (enter to quit): ". The loop will continue until
# while loop evaluates false. Subsequently, when the while loop ends, the value assigned to the count variable will be
# return due to the keyword "return".
def logger(log):
log_entry = input("enter log item (enter to quit): ")
count = 0
while log_entry:
count += 1
log.write(str(count) + ": " + log_entry + "\n")
log_entry = input("enter log item (enter to quit): ")
return count
# [ ] review and run example: makes a blank file (initialize/reset)
# The variable "log_file" is assigned to the return file object from using the open() function to open the file named
# "code_tips.txt" in write and read mode (overwrites), and text mode by default.
log_file = open('log_file.txt', 'w+')
# This line of code removes the reference created by the file open() function that is assigned to the log_file variable, by
# using the file method ".close()".
log_file.close()
# [ ] review and run example - opens the log_file before passing to logger() function call, below
# allows for calls below to run several times appending to the end of log_file
# The variable "log_file" is assigned to the return file object from using the open() function to open the file named
# "code_tips.txt" for appending to end of the file and in read mode (no overwrite), and text mode by default.
log_file = open('log_file.txt', 'a+')
# [ ] review and run example - calls the above logger() function
# what happens running the cell above (a+) again before running this cell again?
# what happens if log_file.seek(0) is run before an append?
# This code calls the logger() function with the argument "log_file", which is a variable, and displays the returned value
# in the output.
logger(log_file)
# This line of code sets the pointer to the beginning of the file assigned to the log_file variable due to the file method
# ".seek()".
log_file.seek(0)
# The variable "log_text" is assigned to the given value when the file "log_file" is attached to the file method ".read()".
log_text = log_file.read()
# This function displays a blank line in the output due to it not containing an argument.
print()
# This variable displays the value assigned to the log_text variable in the output.
print(log_text)
# This line of code removes the reference created by the file open() function that is assigned to the log_file variable, by
# using the file method ".close()".
log_file.close()
# [ ] review and run example - create a file with initial count of 0
# The variable "count_file" is assigned to the return file object from using the open() function to open the file named
# "count_file.txt" in write and read mode (overwrites), and text mode by default.
count_file = open("count_file.txt", "w+")
# This line of code adds the text "Count is: 0" to the count_file file, by using the file method ".write()".
count_file.write("Count is: 0")
# This line of code sets the pointer to the beginning of the file assigned to the count_file variable due to the file method
# ".seek()".
count_file.seek(0)
# This function displays the string form of the first line of the value assigned to the count_file variable due to the file
# method ".readline()", after the leading and trailing whitespace in the value assigned to the count_file variable are
# removed due to it being attached to the .strip() method, in the output.
print(count_file.readline().strip())
# This line of code removes the reference created by the file open() function that is assigned to the count_file variable,
# by using the file method ".close()".
count_file.close()
# [ ] review and run example - can rerun this cell
# The variable "count_file" is assigned to the return file object from using the open() function to open the file named
# "count_file.txt" in read and write mode (no overwrite), and text mode by default.
count_file = open("count_file.txt", "r+")
# This line of code sets the pointer to the beginning of the file assigned to the count_file variable due to the file method
# ".seek()".
count_file.seek(0)
# The variable "count_line" is assigned to the string form of the first line of the value assigned to the count_file
# variable due to the file method ".readline()", after the leading and trailing whitespace in the value assigned to the
# count_file variable are removed due to it being attached to the .strip() method.
count_line = count_file.readline().strip()
# This variable displays the string "BEFORE", a newline due to the escape character "\n", and the value assigned to the
# count_line variable in the output.
print("BEFORE\n" + count_line)
# get the int character(s) after the colon and space, cast and increment
# The variable "count" is assigned to the value given when the 11th character (no other characters after that) is converted
# into an integer, due to the int() function, incremented by the integer "1".
count = int(count_line[10:])+1
# write the incremented value to the file - overwrite before value
# This line of code sets the pointer to the start of 11th character of the file assigned to the count_file variable due to
# the file method ".seek()".
count_file.seek(10)
# This line of code adds the text value assigned to the count variable, after it is changed into a string due to the str()
# function, to the count_file file, by using the file method ".write()".
count_file.write(str(count))
# This line of code sets the pointer to the beginning of the file assigned to the count_file variable due to the file method
# ".seek()".
count_file.seek(0)
# This function displays a newline due to the escape character "\n", the string "AFTER", a newline due to the escape
# character "\n", and the string form of the first line of the value assigned to the count_file variable due to the file
# method ".readline()", after the leading and trailing whitespace in the value assigned to the count_file variable are
# removed due to it being attached to the .strip() method, in the output.
print("\nAFTER\n" + count_file.readline().strip())
# This line of code removes the reference created by the file open() function that is assigned to the count_file variable,
# by using the file method ".close()".
count_file.close()
# [ ] review funtion code for inc_count() funtion that reads file and updates the count
# the file always has 1 line that is The count is: N, where N is an integer
# This program defines a function named "inc_count", that contains the parameter "cnt_file", with a line of code that sets
# the pointer to the beginning of the file assigned to the cnt_file variable due to the file method ".seek()". Additionally,
# the cnt_line variable is assigned the string form of the first line of the value assigned to the cnt_file variable due to
# the file method ".readline()", after the leading and trailing whitespace in the value assigned to the cnt_file variable
# are removed due to it being attached to the .strip() method. Subsequently, the cnt variable is assigned to the value given
# when the 11th character (no other characters after that) is converted into an integer, due to the int() function,
# incremented by the integer "1". Following that, there is a line of code that sets the pointer to the 11th character of the
# file assigned to the count_file variable due to the file method ".seek()". Additionally, there is a line of code that adds
# the text value assigned to the cnt variable, after it is changed into a string due to the str() function, to the cnt_file
# file, by using the file method ".write()". Subsequently, the value assigned to the cnt variable will be return due to the
# keyword "return".
def inc_count(cnt_file):
cnt_file.seek(0,0)
cnt_line = cnt_file.readline().strip()
cnt = int(cnt_line[10:])+1
cnt_file.seek(10,0)
cnt_file.write(str(cnt))
return cnt
# [ ] review and run example with call to function: inc_count() - **Run cell multiple times**
# opens file/prints initial value
# The variable "count_file" is assigned to the return file object from using the open() function to open the file named
# "count_file.txt" in read and write mode (no overwrite), and text mode by default.
count_file = open("count_file.txt", "r+")
# This line of code sets the pointer to the beginning of the file assigned to the count_file variable due to the file method
# ".seek()".
count_file.seek(0)
# The variable "count_line" is assigned to the string form of the first line of the value assigned to the count_file
# variable due to the file method ".readline()", after the leading and trailing whitespace in the value assigned to the
# count_file variable are removed due to it being attached to the .strip() method.
count_line = count_file.readline().strip()
# This variable displays the string "BEFORE", a newline due to the escape character "\n", and the value assigned to the
# count_line variable in the output.
print("BEFORE\n" + count_line)
# call inc_count() to increase the count 5 times
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 0, a stop
# value of 5, and a step value of 1), is iterated. Additionally, the count variable is assigned to the returned value when
# calling the inc_count() function with the arguement "count_file", that is variable. Subsequently, there is a line of code
# that will set the pointer to the beginning of the file assigned to the count_file variable due to the file method
# ".seek()". Following that, a newline due to the escape character "\n", the string "AFTER inc_count() call", the value
# given wehn the value assigned to the i variable is incremented by the integer "1". a newline due to the escape character
# "\n", and the string form of the first line of the value assigned to the count_file variable due to the file method
# ".readline()", after the leading and trailing whitespace in the value assigned to the count_file variable are removed due
# to it being attached to the .strip() method, in the output. This loop will continue until each element generated by the
# range() function is iterated.
for i in range(5):
count = inc_count(count_file)
count_file.seek(0)
print("\nAFTER inc_count() call", i+1, "\n" + count_file.readline().strip())
# This line of code removes the reference created by the file open() function that is assigned to the count_file variable,
# by using the file method ".close()".
count_file.close()
# [ ] complete the task
# The variable "task4_file" is assigned to the return file object from using the open() function to open the file named
# "task4_file.txt" in write and read mode (overwrites), and text mode by default.
task4_file = open('task4_file.txt', 'w+')
# This line of code writes the text "Line1\nLine2\nLine3\n" to the task4_file file, by using the file method ".write()".
task4_file.write("Line1\nLine2\nLine3\n")
# This line of code removes the reference created by the file open() function that is assigned to the task4_file variable,
# by using the file method ".close()".
task4_file.close()
# [ ] code here
# The variable "task4_file" is assigned to the return file object from using the open() function to open the file named
# "task4_file.txt" for appending to end of the file and in read mode (no overwrite), and text mode by default.
task4_file = open('task4_file.txt', 'a+')
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 0, a stop
# value of 5, and a step value of 1), is iterated. Additionally, there is a line of code that adds the text, with the string
# "append #", the value assigned to the item variable after it is changed into a string due to the str() function, and a
# newline due to the escape character "\n", to the task4_file file, by using the file method ".write()". Additionally, there
# is a line of code that sets the pointer to the beginning of the file assigned to the task4_file variable, due to the file
# method ".seek()". This loop will continue until each element generated by the range() function is iterated.
for item in range(5):
task4_file.write("append #" + str(item) +"\n")
task4_file.seek(0)
# This function displays the given value when attaching the task4_file variable to the file method ".read()", in the output.
print(task4_file.read())
# This line of code removes the reference created by the file open() function that is assigned to the task4_file variable,
# by using the file method ".close()".
task4_file.close()
# [ ] complete the task
# The variable "task5_file" is assigned to the return file object from using the open() function to open the file named
# "task5_file.txt" in write and read mode (overwrites), and text mode by default.
task5_file = open('task5_file.txt', 'w+')
# This line of code writes the text "Line\nLine2\nLine3\nLine4\nLine5\nLine6\nLine7\nLine8\nLine9\nLine10\n" to the
# task5_file file, by using the file method ".write()".
task5_file.write("Line\nLine2\nLine3\nLine4\nLine5\nLine6\nLine7\nLine8\nLine9\nLine10\n")
# This line of code that sets the pointer to the beginning of the file assigned to the task5_file variable, due to the file
# method ".seek()".
task5_file.seek(0)
# This function displays the given value by attaching the task5_file varibale to the file method ".read()", and a newline,
# due to the escape character "\n", in the output.
print(task5_file.read(),"\n")
# This line of code removes the reference created by the file open() function that is assigned to the task5_file variable,
# by using the file method ".close()".
task5_file.close()
# [ ] code here
# The variable "task5_file" is assigned to the return file object from using the open() function to open the file named
# "task5_file.txt" in read and write mode (no overwrite), and text mode by default.
task5_file = open('task5_file.txt', 'r+')
# By using the for...in loop, a sequence of integers generated by the range() function (with a start value of 0, a stop
# value of 6, and a step value of 1), is iterated. Additionally, there is a line of code that adds the text, with the string
# "write (r+) #", the value assigned to the item variable after it is changed into a string due to the str() function, and
# a newline due to the escape character "\n", to the task5_file file, by using the file method ".write()". Additionally,
# there is a line of code that sets the pointer in the position given from the value of item variable times the integer "11",
# of the file assigned to the task5_file variable, due to the file method ".seek()". This loop will continue until each
# element generated by the range() function is iterated.
for item in range(1,6):
task5_file.write("write (r+) #" + str(item)+ "\n")
task5_file.seek(item*11)
# This function displays the given value when attaching the task5_file variable to the file method ".read()", in the output.
print(task5_file.read())
# This line of code removes the reference created by the file open() function that is assigned to the task5_file variable,
# by using the file method ".close()".
task5_file.close()