File operations in python
Example to open and read a text file
file_oprtxt.txt
hello guys how are youfile_opr.py (reads the content of file file_oprtxt)
i am fine
fil=open("file_oprtxt","r") #fil is pointer to the filefile_opr.py (write the content in file file_oprtxt)
print(fil.read())
fil.close()
file_opr.py (append the content in file file_oprtxt)#writing content in the file(removes old content of the file and write new content)
fil=open("file_oprtxt","w") #fil is pointer to the file
fil.write("something something meri jaan")
fil.write(" tell me something ek baar ")
fil.close()
file_opr.py (append and read content in file file_oprtxt)#appending content to existing content of file
fil=open("file_oprtxt","a") #fil is pointer to the file
fil.write("kar le hum se batein chaar")
fil.close()
# reading and writing into a file same time
fil = open("file_oprtxt", "r+") # fil is pointer to the file
fil.write("kar le hum se batein chaar")
x=fil.readline()
print(x)
fil.close()
Comments
Post a Comment