#create a file for reading and writing
f=open("myfile.txt", "w+")
print(f.read()) #file is empty, prints ''
print(f.write("stuff \n")) #prints num chars; moves to end of file
print(f.write("more stuff \n")) #prints num chars; moves to end of file
f.seek(0) #returns to start position in file
print(f.read())
f.close()


#compare read, readline, readlines
f=open("myfile.txt", "r")
print(f.read())  #all lines; moves to end of file
f.seek(0)
print(f.readline()) #first line
print(f.readline()) #second line
f.seek(0)
print(f.readlines()) #all lines -> list; moves to end of file
print(f.read()) #blank - nothing more to read
f.seek(0)
print(f.read()) #all
f.close()


#for line in file, print line
f=open("myfile.txt", "r")
for line in f:
    print(line)
f.close()


#read file again without having to go back to start
f=open("myfile.txt", "r")
contents=f.read()  
print(contents) #prints file contents
print(contents) #prints them again
f.close()


#auto closes each time - recommended

with open("myfile.txt", "r") as f:
    print(f.read()) 

with open("myfile.txt", "r") as f:
    print(f.readline()) 
    
with open("myfile.txt", "r") as f:
    print(f.readlines())