Functions of Text file:
1. read() :
●Reads the entire file.
●Read(n) reads n bytes of character from the file.
● It returns the read bytes in the form of a string
Syntax: <variable>=<file handle>.read([n])
Eg: s=f.read()
2. readline():
● Reads a line at a time.
● Readline(n) reads a line at most n bytes
●It returns the read bytes in the form of a string
Syntax: <variable>=<file handle>.readline([n])
Eg: s=f.readline()
3.readlines():
●Reads all lines of a text file and returns them in a list.
Syntax: <variable>=<file handle>.readlines()
Eg: s=f.readlines()
4. write():
●Writes the string given in the argument to the file referenced by <filehandle>
Syntax: <file handle>.write(str)
Eg: f.write(‘This is my file’)
5. writelines():
●Writes all strings of the list given in the argument to the file referenced by
<filehandle>
Syntax: <file handle>.writelines(list)
Eg: f.writelines([‘This is my file’, ‘I am writing text file’])
6. flush():
●To force Python, to write the contents of the buffer onto storage, use flush()
function.
●When the function close() is called implicitly, Python will flush the files
automatically. But the flush() function will flush the data before closing the file.
Syntax: <file handle>.flush()
Eg: f.flush()
Functions of Binary file: