#opening file in read mode
try:
    myFile = open("untitled.txt", "r")
except FileNotFoundError:
    myFile = open("untitled.txt", "a")
    myFile.close()
    myFile = open("untitled.txt", "r")
    

#read whole file
print(myFile.read())


#reset position - will now read the 0th element 
myFile.seek(0)

#read line by line
print(myFile.readline())


#reads next character (1 byte)
print(myFile.read(1))

myFile.seek(0)

#reads next 2 characters
print(myFile.read(2))

myFile.seek(0)

x = myFile.read(1)
#when we get empty string it's end of file
while x != '':
    print(x)
    x = myFile.read(1)

myFile.close()


#opening file in a+ mode allows for append (only adding to end of file)
#and allows for reading file at the same time
myFile = open('untitled.txt','a+')
myFile.write('\nhey')
myFile.seek(0)
print(myFile.read())
myFile.close()


#opening in write mode will ERASE THE INFO IN FILE!
#can be used to overwrite text in file
myFile = open('untitled.txt','w')
myFile.write('hey')
myFile.close()