file open modes python 3 code example

Example 1: python write to file

with open(filename,"w") as f:
  f.write('Hello World')

Example 2: python file reading

fin = open("NAME.txt", 'r')
body = fin.read().split("\n")
line = fin.readline().strip()

Example 3: python file open

#there are many modes you can open files in. r means read.
file = open('C:\Users\yourname\files\file.txt','r')
text = file.read()

#you can write a string to it, too!
file = open('C:\Users\yourname\files\file.txt','w')
file.write('This is a typical string')

#don't forget to close it afterwards!
file.close()