What is file in Python? Explain
the methods to open,close the file in Python?
Ans:-A file is some information or data which stays in the
computer storage device.Python gives you easy ways to manipulate these files.
OPENING A FILE:-Python provides the open()function which
accepts two arguments,file name,access mode in which the file is accessed.The
function returns a file object which can be used to perform various operations
like reading writing etc.
SYNTAX:-
File object=open(<file-name>,<access-mode>)
EXAMPLE:-
fileptr=open(“file.txt”,”r”)
if fileptr:
print(“file
is opened successfully”)
CLOSING A FILE:-Once all the operations are done on the
file,we must close it through our python script using the close()method.
SYNTAX:-
fileobject.close()
EXAMPLE:-
fileptr=open(“file.txt”,”r’)
if fileptr:
print(“file
is opened successfully”)
fileptr.close()