# Noemi Cabrera
# 12 November 2021
# In this lesson, I learned how to use .seek() to set a file read or write location and append
# mode to add text to the end of a file automatically. Aditionally, I practice using the other
# write modes I learned in Module 7. These 2 methods I learned are very useful to simplify code.
# The append is useful so that you don't have to use the seek() method again to set the pointer
# to the end of a file.
# I didn't have any difficulties.
# [ In this code, the 'w' mode used in open() erases everything new_file.txt file.
# Now, an empty file, whic you can write on, is stored in the new_file variable ] review and run example
# - creates a new local file or overwrites the local file (makes it blank)
new_file = open('new_file.txt', 'w')
# [ In this code, text is written into the new file using the .write() method. ]
# review and run example to write some text to the file
new_file.write("This is line #1 with 'w'\nThis is line #2 with 'w'\nThis is line #3 withn 'w'!\n")
# [ In this code, the new_file.txt is closed using the .close() method and he re-opened in
# read mode using open(). ] review and run example
# - close file and re-open in read mode
# - head pointer is at start of file
new_file.close()
new_file = open('new_file.txt', 'r')
# [ In this code, the new_file.txt is read using the .read() method. The text of the file is
# printed, and then the file is closed using .close(). ]
# review and run example to see what was written to the file
new_text = new_file.read()
print(new_text)
new_file.close()
# [ In this code, the inner_planets.txt local file is created by using the open() method.
# The file is opened in 'write' mode, therefore, the file is empty. The local file
# is stored in the in_planets variable. ] open planets.txt in write mode
in_planets = open('inner_planets.txt','w')
# [ In this code, using the .write() method, the 1st four planets of the solar system are
# written as text for the in_planets opened file. Since each planet has the '\n' format, they
# will be printed in a new line. ] write Mercury, Venus, Earth, Mars on separate lines
in_planets.write("Mercury\nVenus\nEarth\nMars")
# [ In this code, the in_planets opened file is closed using .close(). Then, this file is
# re-opened in read mode using open() with the 'r' mode.] close the file and re-open in read mode
in_planets.close()
in_planets = open('inner_planets.txt','r')
# [ In this code, the in_plents file is read using the .read() method. This is stored in the
# planets_text variable. ] use .read() to read the entire file contents
planets_text = in_planets.read()
# [ In this code, the contents of the in_planets file is displayed and then the file is closed
# using .close(). ] print the entire file contents and close the file
print(planets_text)
in_planets.close()
# [ In this code, the local file 'new_file.txt' is created using open().The file is opened
# in 'write + read' mode, therefore, the file is empty and you can read and write in it. The
# local file is stored in the new_file variable.] review and run example
# creates a new local file or overwrites the local file (makes it blank)
new_file = open('new_file.txt', 'w+')
# [ In this code, the read() method is used to read new_file.txt. This is stored in the new_text
# variable and then this is displayed. ] review and run example to see what was written to the file
# - 'w+' overwrites, we can read, but the file is ***BLANK***
new_text = new_file.read()
print(new_text)
# [ In this code, the .write() mthod is used to load/write text in the new file created
# that is blank. ] review and run - write to the blank file
new_file.write("This is line #1 with 'w+'\nThis is line #2 with 'w+'\n")
# [ In this code, the read() method is used to read the new_file.txt. This is stored in the
# new_text variable and then printed. Since the 'w' mode puts the cursor at the end of the
# file, the output will be an empty string.]
# review and run example - read and print (Note: the pointer is at the end of the file - result = empty string)
new_text = new_file.read()
print(new_text)
# [ In this code, the seek(0) method is used to move the pointer to the beginning of new_file.
# To move to the beginning, the argument has to be 0 since the 1st character is at index 0.]
# review and run example - sets pointer to beginning of file
new_file.seek(0)
# [In this code, the read() method is used to red the new_file. This is stored in the new_text
# variable. Since in the above code the pointer was moved to the beginning, the entire file
# is read and the text is printed. ] review and run example - now read starts from beginning of file
new_text = new_file.read()
print(new_text)
# [ In this code, the new_file is closed using the .close method. ] review and run example - clean up and close file
new_file.close()
# [ In this code, the local file "outer_planets.txt" is created with the "w+" mode, which
# creates a a new blank file that you can write and read. ] open outer_planets.txt in write mode 'w+'
outer_planets = open('outer_planets.txt','w+')
# [ In this code, the write() method is used to write/load text in the outer_planets file
# that is empty. ]
# write four outer planets in earth's solar system (Jupiter, Saturn, Uranus, Neptune) on separate lines
outer_planets.write('Jupiter\nSaturn\nUranus\nNeptune')
# [ The seek() method is used to move the pointer to the beginning of the outer_planets file.]
# use .seek() to move the pointer to the start of the file
# [ The read() method is used to read the entire outer_planets file from the beginning]
# use .read() to read the entire file contents
outer_planets.seek(0)
oplanets_text = outer_planets.read()
# [ The contents of the outer_plantes file is printed and then the file is closed using
# .close() ] print the entire file contents and close the file
print(oplanets_text)
outer_planets.close()
# [ A local file, named code_tips, is created by using the open() method. Since the 'w+'mode
# is used, you can write and read the file. Then , write() is used to load text into the empty
# file and the seek(0) is used to put the pointer at the beginning of the file. Lastly, the
# file is read using read() and its contents are displayed.]
# review and run - create, write, read and print a file
tips_file = open('code_tips.txt', 'w+')
tips_file.write('-use simple function and variable names\n-comment code\n-organize code into functions\n')
tips_file.seek(0)
tips_text = tips_file.read()
print(tips_text)
# [ In this code, the seek() method was used to move the pointer to the 14th character(index 13)
# of the tips_file. Then , the tips_file is read using the read() method and the text starting
# at index 13 til the end of the file is displayed.] review and run example - setting a specific seek() index
tips_file.seek(13)
# now read starts from 14th character of file
tips_text = tips_file.read()
print(tips_text)
# [ In this code, the tips_file text is displayed starting at index 13 is displayed using
# string slicing. First, the pointer is set to the beginning of the file using seek() and
# then the entire file is read using read(). To print starting at index 13, the
# tips_text[13:] string slice was used.] review and run example - string slicing on a read of an entire file
# read from the start
tips_file.seek(0)
tips_text = tips_file.read()
# slice from the 14th character to end
print(tips_text[13:])
# [ In this code, the seek() method is used with an offset argument of 0 and a whence argument
# of 2 to place the pointer at the end of the file. Then text is written into tips_file
# using .write(). Using seek again, the pointer is to the beginning of the file and then
# the file is read using read(). The contents of tips_file are displayed. ]
# review and run example - setting pointer to end of file with whence value = 2
tips_file.seek(0,2)
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)
tips_file.seek(0,0)
tips_text = tips_file.read()
print(tips_text)
# [ In this code, the pointer is set to the beginning of the tips_file and then the first
# line is overwritten as all uppercase because the file was opened in 'w+' mode. ] review and run example - point to file beginning and overwrite 1st line
tips_file.seek(0)
tips_file.write('-use simple function and variable names\n'.upper())
# [ In this code, the pointer is set to the beginning of tips_file and then the file is read
# using read(). This is stored in the tips_text variable and then the modified contents of the
# file are printed. ] review and run example - show new file contents
tips_file.seek(0,0)
tips_text = tips_file.read()
print(tips_text)
# In this code, a new local file is created using the open() method. The file is named
# days.txt and it's on "w+" mode. This is stored in the days_file variable.
# [ ] open a new file days.txt in write plus read mode 'w+'
# [ ] write week days (Monday - Friday) on separate lines to the file
days_file = open("days.txt","w+")
days_file.write("Monday\nTuesday\nWednesday\nThursday\nFriday")
# In this code, the seek() method is used to move the pointer to the beginning of the
# days_file. Then, read() is used to read the contents of the file and they are displayed
# using print().
# [ ] 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
days_file.seek(0)
days_text = days_file.read()
print(days_text)
# In this code, the seek() method is used with both offset and whence arguments. A whence
# value of 2 puts the pointer at the end of the file, and the offset value has to be zero to
# do this. Then the write() method is used to writein the days_file. Since the pointer is now
# at the end, the new text will be placed at the end of the file.
# [ ] use .seek() to move the pointer to the end of the file
# [ ] write the weekend days (Saturday & Sunday)
days_file.seek(0,2)
days_file.write("\nSaturday\nSunday")
# In this code, the seek() method is used to move the pointerto the beginning of the file
# and then the read() method reads all the contents of the days_file. The contents are
# displayed using print and then the file is closed using close().
# [ ] 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
days_file.seek(0)
days_text = days_file.read()
print(days_text)
days_file.close()
# In this code, the logger function is defined and has 1 parameter (log). This function gets
# user input for a long item. The using a while loop, the count variable is added 1 every
# time there is an item entered and the write() method is used to write on the log parameter
# of the function. The logger function returns the value of the variable count.
# [ ] review and run example - function writes to the open log argument
# loads funtion into memory but the funtion is not called
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
# In this code, the open() method creates a local file in 'w+' mode named log_file.txt. This
# is stored in the log_file variable and the file is closed using .close()
# [ ] review and run example: makes a blank file (initialize/reset)
log_file = open('log_file.txt', 'w+')
log_file.close()
# In this code, the log_file.txt is opened in 'a+' mode using the open() method. You can read
# and write on this file, but you can't overwrite the entire file.
# [ ] 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
log_file = open('log_file.txt', 'a+')
# In this code, the logger function defined above is called with the opened log_file above
# as its argument. The seek() method is used to set the pointer to the begining of the log_file.
# The read() method reads the entire file and this is stored in the log_text variable. Lastly,
# the contents of the file are printed and the file is closed.
# [ ] 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?
logger(log_file)
log_file.seek(0)
log_text = log_file.read()
print()
print(log_text)
log_file.close()
# [ In this code, a local file is created in "w+" mode using the open() method. The file is
# named count_file.txt and then this is stored in the count_file variable. The file is closed using .close().]
# review and run example - create a file with initial count of 0
count_file = open("count_file.txt", "w+")
count_file.write("Count is: 0")
count_file.seek(0)
print(count_file.readline().strip())
count_file.close()
# [ In this code, a local file is created in 'r+'mode using the open() method and the file is
# named count_file.txt. This is stored in the count_file variable. Then , the seek() method
# is used to move the pointer to the begining of the file. Using the readline() method, the
# file is read line by line loading each one as a string and then any leading and tracking
# white spaces are removed from the text because the strip() method is used. ] review and run example - can rerun this cell
count_file = open("count_file.txt", "r+")
count_file.seek(0)
count_line = count_file.readline().strip()
print("BEFORE\n" + count_line)
#[String slicing is used to acess the text of the count_file statrting at index 10 until the
# last index of the file. This is changed to integer type and then added 1. This stored in
# the "count" variable.]
# get the int character(s) after the colon and space, cast and increment
count = int(count_line[10:])+1
# [Using seek(0, the pointer is moved to index 10 of the count_file, and the write() method
# is used to add the value of the count variable as a string.]
# write the incremented value to the file - overwrite before value
count_file.seek(10)
count_file.write(str(count))
# [The pointer is moved back to the begining of the count_file and readline() is used to read
# the entire file line by line. ]
count_file.seek(0)
print("\nAFTER\n" + count_file.readline().strip())
# [The file is closed using .close()]
count_file.close()
# [ In this code, the inc_count function is defined. This function has 1 parameter(cnt_file).
# Using the seek() method, the pointer of the cnt_file is set to the begining. Then, the file
# is read line by line using the readline() method and the white spaces are removes using strip().
# This is stored in the cnt_line variable. String slicing is used to acess the text of the
# cnt_file starting at index 10 until the last index of the file. This is changed to integer type
# and then added 1. This stored in the "cnt" variable.Lastly, the pointer is set to index 10 of the
# cnt_file and then the contents of the variable cnt are written in this file as a string. This inc_count
# function returns the value of the variable cnt. ]
# 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
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
# [ In this code, a local file is created in 'r+' mode using the open() method and the file is
# named count_file.txt. This is stored in the count_file variable. Then , the seek() method
# is used to move the pointer to the begining of the file. Using the readline() method, the
# file is read line by line loading each one as a string and then any leading and tracking
# white spaces are removed from the text because the strip() method is used. ] review and run example with call to function: inc_count() - **Run cell multiple times**
# opens file/prints initial value
count_file = open("count_file.txt", "r+")
count_file.seek(0)
count_line = count_file.readline().strip()
print("BEFORE\n" + count_line)
# [A for..in loop is used to call the inc_count previously defined function 5 times since the
# range is 5. The count_file is entered as the argument for the function. The contents of the
# count_file are printed after the function is called. ]
# call inc_count() to increase the count 5 times
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())
# [The count_file is closed]
count_file.close()
# In this code, the task4_file is first opened in 'w+' mode and text is written in the empty
# file using the write() method. The file is closed. Then, the file is opened in 'a+' mode.
# A for..in loop is used to add text to the end of the file 4 times since range(5) doesn't include
# the 5. Lastly, the contents of the file are printed.
# [ ] complete the task
task4_file = open('task4_file.txt', 'w+')
task4_file.write("Line1\nLine2\nLine3\n")
task4_file.close()
# [ ] code here
task4_file = open('task4_file.txt', 'a+')
for item in range(5):
task4_file.write("append #"+ str(item)+"\n")
task4_file.seek(0)
print(task4_file.read())
# # In this code, the task5_file is first opened in 'w+' mode and text is written in the empty
# file using the write() method. The pointer is set to the begining of the file using seek(0)
# so that the entore file cann be read using read(). The file is closed. Then, the file is opened
# in 'r+' mode. A for..in loop is used to add text to the task5_file and the pointer is set to 11
# times the loop count. Lastly, the contents of the file are printed.
# [] complete the task
task5_file = open('task5_file.txt', 'w+')
task5_file.write("Line\nLine2\nLine3\nLine4\nLine5\nLine6\nLine7\nLine8\nLine9\nLine10\n")
task5_file.seek(0)
print(task5_file.read(),"\n")
task5_file.close()
# [ ] code here
task5_file = open('task5_file.txt','r+')
for item in range(1,6):
task5_file.write("write (r+) #"+ str(item)+"\n")
task5_file.seek(item*11)
print(task5_file.read())