File handling in python





Mode Explain
"r" Read the file
"a" Append the file
"w" Write the file
"x" Create the file
"t" Text mode - Default Mode
"b" Binary mode







Create file



"x"--> It is used to create a python file.




Example
f = open("file.txt", "x")







Write file



write()--> It is used to write a python file.




Example
f = open( "file.txt" , "w")

f.write("Welcome to Mycrazycoding")

f.close()







Read file



Read-->f.read()--> It is used to read a python file.




Example
f = open("file.txt")

print(f.read())







Delete file



Delete-->remove()--> It is used to delete a python file.




Example
import os

os.remove("file.txt")