file reading python code example
Example 1: python read file
with open("file.txt", "r") as txt_file:
return txt_file.readlines()
Example 2: python open file
def main():
f= open("guru99.txt","w+")
#f=open("guru99.txt","a+")
for i in range(10):
f.write("This is line %d\r\n" % (i+1))
f.close()
#Open the file back and read the contents
#f=open("guru99.txt", "r")
#if f.mode == 'r':
# contents =f.read()
# print (contents)
#or, readlines reads the individual line into a list
#fl =f.readlines()
#for x in fl:
#print(x)
if __name__== "__main__":
main()
Example 3: how to open a file in python
f = open("demofile.txt")