#Samagra Bhattarai 11/15/21
#In this task I used the seek command to direct the pointer, and write() to write in the file
#as well as explored new modes such as append a+,
#One diffulty I encountered was in task 3 where it asks to close the code but nevers aks to re open so I closed the code further down the task
# [ ] review and run example
# - creates a new local file or overwrites the local file (makes it blank)
#in this code we use the same open command as we did in the rpevious lesson but we know open it in write mode.
new_file = open('new_file.txt', 'w')
# [ ] review and run example to write some text to the file
#We can use the .write() command in order to add some text to the blank file.
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
#in this code we close() the file and then we re-open it using read mode.
new_file.close()
new_file = open('new_file.txt', 'r')
# [ ] review and run example to see what was written to the file
#In this code we print the contents of the new file, and see that the original content has been
#deleted and replaced with what we wrote in the file.
new_text = new_file.read()
print(new_text)
new_file.close()
# [ ] open planets.txt in write mode
#In this code we open innerplanets.txt in write mode which deleted all of the content inside of it.
inner_planets = open('inner_planets.txt','w')
# [ ] write Mercury, Venus, Earth, Mars on separate lines
#In this cde we use the write command to write the first 4 planets in the solar system on seperate lines into the file
inner_planets.write("Mercury\nVenus\nEarth\nMars")
# [ ] close the file and re-open in read mode
#closing the file using .close() then re-opening it in read mode.
inner_planets.close()
inner_planets = open('inner_planets.txt','r')
# [ ] use .read() to read the entire file contents
#using .read() to read the entirity of the the files contents and assign it to variable planets.
planets = inner_planets.read()
# [ ] print the entire file contents and close the file
# printing the variable we created in our previous code, and then closing the inner planets file.
print(planets)
inner_planets.close()
# [ ] review and run example
# creates a new local file or overwrites the local file (makes it blank)
#in this code we open the file in w+ mode which is w but now we can also read
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***
#Because we are using w+ we can read and print the contents in the file, but the file by default will be blank after we open with w+
new_text = new_file.read()
print(new_text)
# [ ] review and run - write to the blank file
#In this code we write text into new_file
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)
#In this code we read the contents of the file and then print, however it returns empty as the pointer is at the end of the string.
new_text = new_file.read()
print(new_text)
# [ ] review and run example - sets pointer to beginning of file
# in this code we use .seek(0) to place the the pointer at the start of the file
new_file.seek(0)
# [ ] review and run example - now read starts from beginning of file
#Before the pointer was at the end of the file, now that it is at the start the entirety of the file is printed.
new_text = new_filOne difficue.read()
print(new_text)
# # [ ] review and run example - clean up and close file
#in this code we close new_file.
new_file.close()
# [ ] open outer_planets.txt in write mode 'w+'
#in this code we open the outer plantets text in w+ mode
outer_planets = open('outer_planets.txt','w+')
# [ ] write four outer planets in earth's solar system (Jupiter, Saturn, Uranus, Neptune) on separate lines
#In this code we write the last 4 planets in our solar system on seperate lines into outer_planets
outer_planets.write('Jupiter\nSaturn\nUranus\nNeptune')
# [ ] use .seek() to move the pointer to the start of the file
# [ ] use .read() to read the entire file contents
#In this code we set the pointer to the start of the file using seek(0) then read the file of the contents and put in outerplanets
outer_planets.seek(0)
outerplanets = outer_planets.read()
# [ ] print the entire file contents and close the file
# in this code we print outerplanets variable we created then close the file
print(outerplanets)
outer_planets.close()
# [ ] review and run - create, write, read and print a file
# in this code, code_tips.txt is opened in w+ and then a message is written, then the pointer is moved to the start and the output of the file is show
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)
# [ ] review and run example - setting a specific seek() index
#In this code we set the pointer to start reading from the 14th charector, and then print the contents of the file
tips_file.seek(13)
# now read starts from 14th character of file
tips_text = tips_file.read()
print(tips_text)
# [ ] review and run example - string slicing on a read of an entire file
# read from the start
#In this coder we read the entire file however we slice the file so that it only prints from the 14th charector
tips_file.seek(0)
tips_text = tips_file.read()
# slice from the 14th character to end
print(tips_text[13:])
# [ ] review and run example - setting pointer to end of file with whence value = 2
#In this code we use the (0,2) to set the pointer at the end of the file with the whence value 2, then we write an message onto the file and print contents
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)
# [ ] review and run example - point to file beginning and overwrite 1st line
#In this code we place the seek to 0 and then we replace the first line with an uppercase version of the same contents
tips_file.seek(0)
tips_file.write('-use simple function and variable names\n'.upper())
# [ ] review and run example - show new file contents
#In this code we set the pointer to the start of the file and then print the contents of the file after the first l ine has been replaced
tips_file.seek(0,0)
tips_text = tips_file.read()
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
#in this coder we open up days.txt in w+ mode then write the days of the week not saturday and sunday on seperate lines
days_file = open("days.txt","w+")
days_file.write("Monday\nTuesday\nWednesday\nThursday\nFriday")
# [ ] 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
#In this code we use seek to move the pointer to the start of the file then read the file, print its contents
days_file.seek(0)
days_text = days_file.read()
print(days_text)
# [ ] use .seek() to move the pointer to the end of the file
# [ ] write the weekend days (Saturday & Sunday)
#In this code we move the pointer to the end of the file then write two additional days saturday and sunday on seperate lines
days_file.seek(0,2)
days_file.write("\nSaturday\nSunday")
# [ ] 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
#in this code we once again move the pointer to the start, then read the file before printing, this time we close the file.
days_file.seek(0)
days_text = days_file.read()
print(days_text)
days_file.close()
# [ ] review and run example - function writes to the open log argument
# loads funtion into memory but the funtion is not called
#in this code a while loop is created for the function logger, that logs a item by writing it to the end of the file
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)
#in this code we open the file inw+ moe which resets the contents of the file then we close it
log_file = open('log_file.txt', 'w+')
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
#in this code we re open the log file in a+ mode that allows us to run the cell several times to append to log_file
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?
#In this code the function logger is used, the pointer for the log file is set to 0 and then our inputs are appended and printed.
#Running the last cell again does not clear the contents but rather when we run this code we can still add inputs.
logger(log_file)
log_file.seek(0)
log_text = log_file.read()
print()
print(log_text)
log_file.close()
# [ ] review and run example - create a file with initial count of 0
#in this file we open an txt file in w+ mode and then write "Count is :0 " before setting the pointer to the start and printing
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()
# [ ] review and run example - can rerun this cell
#in this code the count file is opened in r+ mode and the pointer is set to the start before the contents are printed, then the
# code gets to acsess the test starting at index 10 until the last index of the file. This is changed to integer type and then added 1.
#We then seek ten write the count variable and then seek 0 and print the after of the changes.
count_file = open("count_file.txt", "r+")
count_file.seek(0)
count_line = count_file.readline().strip()
print("BEFORE\n" + count_line)
# get the int character(s) after the colon and space, cast and increment
count = int(count_line[10:])+1
# write the incremented value to the file - overwrite before value
count_file.seek(10)
count_file.write(str(count))
count_file.seek(0)
print("\nAFTER\n" + count_file.readline().strip())
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
#In this code a function inc_count is made, the pointer is set to 0 and the file is readlined an stripped then the 10th index beyond
# read and is turned into a intger before it gets 1 added,we seek the 10th index and then write the count before returning cnt
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
#in this code we open count file in r+ mode then we seek 0 and then readline and strop, we print the before, then we create a loop
# that runs the function inc_count 5 time and then prints the result of each run, showing that the value increases by 1 each run
count_file = open("count_file.txt", "r+")
count_file.seek(0)
count_line = count_file.readline().strip()
print("BEFORE\n" + count_line)
# 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())
count_file.close()
# [ ] complete the task
#in this code the file is first open in w+ mode and 3 lines are written onto it before closing, then we re open the file in a+ mode
# we then create a loop that writes to the file and sets the pointer to 0 5 times, we then read and print the file
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())
# [ ] complete the task
#in this code the file is opened in w+ mode and then 10 lines are written, the pointer is set to 0 and the contents of the file are printed
#before it is cloded. then we open the file in r+ mode and then create a loop in range 1,6 that writes the to the file write (r+) #"+ str(item)+"\n"
# and seeks the item number*11 6 times. then we print the output.
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())