- flush() method is used for flushes the internal buffer.
- While writing the file operating system store the content of the file in the buffer
and buffer content should be written into a file using
- flush() method
- when the buffer is full
- python automatically flushes the files when closing them. but when we want to flush the data before closing them then the python built-in function .flush() is used
Syntax:
fileobj.flush()
Example:
#initialize file
fileobj=open("mydata.txt","w")
#Write content to file
fileobj.write("New Content Added")
#flush the Content buffer to file
fileobj.flush()
- fileno returns the integer file descriptor that is used by the implementation to request i/o operation
- the file descriptor is simply indexed into a file descriptor table
- for each process in the operating system, there is one block PCB(process control block)
- one of the parts of PCB keep track context of the process which is array structure called file descriptor
Syntax:
fileobj.fileno()
Example:
#initialize file
fileobj=open("mydata.txt","w")
#get file descriptor index value
index_val=fileobj.fileno()
print(index_val)
Output:
3
- isatty() return True if a file is connected with terminal device(tty)
Syntax:
fileobj.isatty()
Example:
#initialize file
fileobj=open("mydata.txt","w")
#Check whether file is connected with tty or not
val=fileobj.isatty()
print(val)
Output:
False
- set the file cursor current position
Syntax:
fileobj.seek(position)
File myfile.txt contains
Hi, I am writing simple python programm.
Example:
#initilize the file
file=open("myfile.txt","r")
#this will set file reading cursor to 6 th position
position=file.seek(6)
#read content
content=file.read()
print(content)
Output:
am writing simple python programm.
You use 0 as a position to set file reading cursor to the first position at the starting of file
- this method tell the current exact file cursor position in integer
Syntax:
Fileobj.tell()
File myfile.txt contains
Hi, I am writing simple python programm.
Example:
#initilize the file
file=open("myfile.txt","r")
#read first 5 characters
data=file.read(5)
#now get the current position of file cursor
pos=file.tell()
Output:
5
1.read()
- read the bytes from the file.
2.readline()
- read the single line from file
3.readlines()
- read all lines in file and return list object.
1.write()
- used to write content to the file
2.writelines()
- used to write multiple lines to the file list structure is used.