Ad Code

6 Python File Methods to Read Right Now

These are six file functions(methods) in python. These you can use while working with files. Understanding these will help for your interviews. Check out here 5 top file modes in python.


File methods

File methods 

Below are the top six file methods in python:
  1. Tell()
  2. Seek()
  3. Flush()
  4. Fileno()
  5. truncate()
  6. isatty()

Syntax and purpose


tell()


Purpose: This method will return the cursor position of the file pointer from the beginning of the file. It will tell the current cursor position. The first character index in files is zero just like the string index.


Syntax: 

fileobject.tell()


seek()


Purpose: This method will move the cursor position from one position to the specified position from the beginning of the file. Here, the cursor will be sought to a particular location.

Syntax: 

fileobject.seek(position)

flush()


Purpose: This method will clean out the internal buffer. Before writing the text in the file, it is best practice to clear out that the internal buffer can be cleared.

Syntax: 

fileobject.flush()

fileno()


Purpose: This method will return the file number i.e. a file descriptor as an integer value. If an operating system does not use a file descriptor of the file then an error may occur.

Syntax: 

fileobject.fileno()

truncate()


Purpose: This method will resize the file based on the given number of bytes. The current position will be used if the size is not specified.

Syntax: 

fileobject.truncate(size)

isatty()


Purpose: This method will return True if the file is connected to an end device like (tty) device else will return False.

Syntax:

fileobject.isatty()

Related

Post a Comment

0 Comments