#Briley Jackson
#nov 2 2021
#U2M4A4.4
#finally learning .write() and .seek methods
# [ ] review and run example
# - creates a new local file or overwrites the local file (makes it blank)
new_file = open('new_file.txt', 'w')
# [ ] 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")
# [ ] 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')
# [ ] review and run example to see what was written to the file
new_text = new_file.read()
print(new_text)
new_file.close()
# [ ] open planets.txt in write mode
planets=open('inner_planets.txt','w')
# [ ] write Mercury, Venus, Earth, Mars on separate lines
planets.write('Mercury\nVenus\nEarth\nMars')
# [ ] close the file and re-open in read mode
planets.close()
planets=open('inner_planets.txt','r')
# [ ] use .read() to read the entire file contents
x=planets.read()
# [ ] print the entire file contents and close the file
print(x)
planets.close()
# [ ] review and run example
# creates a new local file or overwrites the local file (makes it blank)
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***
new_text = new_file.read()
print(new_text)
# [ ] review and run - write to the blank 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)
new_text = new_file.read()
print(new_text)
# [ ] review and run example - sets pointer to beginning of file
new_file.seek(0)
# [ ] review and run example - now read starts from beginning of file
new_text = new_file.read()
print(new_text)
# # [ ] review and run example - clean up and close file
new_file.close()
# [ ] open outer_planets.txt in write mode 'w+'
morePlanets=open('outer_planets.txt','w+')
# [ ] write four outer planets in earth's solar system (Jupiter, Saturn, Uranus, Neptune) on separate lines
morePlanets.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
morePlanets.seek(0)
y=morePlanets.read()
# [ ] print the entire file contents and close the file
print(y)
morePlanets.close()
# [ ] 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)
# [ ] 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)
# [ ] 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:])
# [ ] 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)
# [ ] 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())
# [ ] review and run example - show new file contents
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
days=open('days.txt','w+')
days.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
days.seek(0,0)
weekdays=days.read()
print(weekdays)
#days.close()
# [ ] use .seek() to move the pointer to the end of the file
# [ ] write the weekend days (Saturday & Sunday)
#days=open('days.txt','w+')
days.seek(0,2)
days.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
days.seek(0,0)
alldays=days.read()
print(alldays)
days.close()
# [ ] 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
# [ ] review and run example: makes a blank file (initialize/reset)
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
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?
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
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
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
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
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
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)
any=task4_file.read()
print(any)
# [ ] 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)
blah=task5_file.read()
print(blah)