#John Bible 11/2/21
#What I learned was how to use write and seek.
#What I had trouble with was either nothing or task 5 as I don't know if it was done correctly.
# [ ] 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
#This will open planets.txt in write mode
planets=open("planets.txt","w")
# [ ] write Mercury, Venus, Earth, Mars on separate lines
#This will write Mercury, Venus, Earth, Mars on separate lines
planets.write("Mercury\nVenus\nEarth\nMars")
# [ ] close the file and re-open in read mode
#This will close the file and re-open in read mode
planets=planets.close
planets=open("planets.txt","r")
# [ ] use .read() to read the entire file contents
#This will use .read() to read the entire file contents
planetary=planets.read()
# [ ] print the entire file contents and close the file
#This will print the entire file contents and close the file
print(planetary)
# [ ] 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+'
#This will open outer_planets.txt in write mode 'w+'
outer_planets=open("outer_planets.txt","w+")
# [ ] write four outer planets in earth's solar system (Jupiter, Saturn, Uranus, Neptune) on separate lines
#This will write Jupiter, Saturn, Uranus, Neptune on separate lines
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
#This will use .seek() to move the pointer to the start of the file and then use .read() to read the entire file contents
outer_planets_run=outer_planets.seek(0)
outer_planets_run=outer_planets.read()
# [ ] print the entire file contents and close the file
#This will print the entire file contents and close the file
print(outer_planets_run)
outer_planets.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
#This will open a new file days.txt in write plus read mode 'w+' and then write Mon-Fri on seperate lines to the file
days_text=open("days.txt","w+")
days_text.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
#This will use .seek() to move the pointer to the start of the file and then use .read() to read the entire file contents before printing the contents and closing the file
days_text_run=days_text.seek(0)
days_text_run=days_text.read()
print(days_text_run)
days_text.close
# [ ] use .seek() to move the pointer to the end of the file
# [ ] write the weekend days (Saturday & Sunday)
#This will use .seek() to move the pointer to the end of the file and write Saturday & Sunday
days_text.seek(0,2)
days_text.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
#This will use .seek() to move the pointer to the start of the file and then use .read() to read the entire file contents before printing the contents and closing the file
days_text.seek(0)
days_text_run=days_text.read()
print(days_text_run)
days_text.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
#This will task4_file in append plus mode and the loop will send results into the file
task4_file = open('task4_file.txt', 'a+')
def loop(put):
item=input("Enter a number here unti you are right:")
for item in range(5):
item=input("Enter a number here unti you are right:")
if item in range(5):
put.write(item + " is correct!" + "\n")
else:
put.write(item + " is not correct." + "\n")
loop(task4_file)
task4_file.seek(0)
task4_file_r=task4_file.read()
print(task4_file_r)
# [ ] 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
#This will open task5_file.txt in read plus mode and go through a loop to add to the file
task5_file=open("task5_file.txt","r+")
for item in range(1,6):
number=item
task5_file.write("write (r+) #"+str(number)+"\n")
task5_file.seek(item*11)
task5_file.seek(0)
task5_file_r=task5_file.read()
print(task5_file_r)