Menu Close

Python File Object Methods

This is the most important tutorial on Python file handling because, throughout this article, we will explain to you all the Python file object methods that are very helpful to work with files in Python.

In previous tutorial we have seen following Python file handling topics.

Python file object.

Python built-in open() function is used to open the file from the server and local server and always return a file object.

import os
#open myfile.txt with read mode.f is the file object
f = open("myfile.txt", "r")

close() method

close() method is used to close the open file. It is the good practice to close a opened file.

import os
#open myfile.txt with read mode.f is the file object
f = open("myfile.txt", "r")

#close the file
f.close()

detach() method

This method is used to separate raw stream from the buffer.

import os
#open myfile.txt with read mode.f is the file object
f = open("myfile.txt", "r")

b = f.detach()
print(b)

filno() method

filno() method is used to return the integer number of the file.

import os
#open myfile.txt with read mode.f is the file object
f = open("myfile.txt", "r")

num = f.fileno()
print(num)

flush() method

flush() method is used to flush the internal buffer.

import os
#open myfile.txt with read mode.f is the file object
f = open("myfile.txt", "a")
f.flush()
f.close()

isatty() method

This method is used to return True if the file is interactive.

import os
#open myfile.txt with read mode.f is the file object
f = open("myfile.txt", "a")
print(f.isatty())
f.close()

read() method

This is the most important method. read() method is used to read the contents from the file.

import os
#open myfile.txt with read mode.f is the file object
f = open("myfile.txt", "a")
print(f.read())
f.close()

readable() Method

Return True if the file system readable otherwise False.

import os
#open myfile.txt with read mode.f is the file object
f = open("myfile.txt", "a")
print(f.readable())
f.close()

readline() method

file readline() method is used to read only one line from the file.

import os
#open myfile.txt with read mode.f is the file object
f = open("myfile.txt", "a")
print(f.readline())
f.close()

readlines() method

File readlines() method returns list contains all the lines.

import os
#open myfile.txt with read mode.f is the file object
f = open("myfile.txt", "a")
print(f.readlines())
f.close()

seek(offset) method

seek() method is used to change the current file position.

import os
#open myfile.txt with read mode.f is the file object
f = open("myfile.txt", "r")
f.seek(30)
print(f.read())
f.close()

seekable() method

seekable() method will return True if the file system allow to change the position.

import os
#open myfile.txt with read mode.f is the file object
f = open("myfile.txt", "r")
print(f.seekable())
f.close()

tell() method

tell() method returns the current file position.

import os
#open myfile.txt with read mode.f is the file object
f = open("myfile.txt", "r")
pos = f.tell()
print(pos)
f.close()

truncate(size) method

truncate() method is used to truncate the file from the given number of bytes.

import os
#open myfile.txt with read mode.f is the file object
f1 = open("myfile.txt", "a")
f1.truncate(40)
f1.close()
#open and read the file after truncate
f2 = open("myfile.txt", "r")
print(f2.read())

writable() method

Return True if the file allow to write.

import os
#open myfile.txt with read mode.f is the file object
f = open("myfile.txt", "a")
print(f.writable())
f1.close()

write(string) method

write() method is used to write the file system.

import os
#open myfile.txt with read mode.f is the file object
f = open("myfile.txt", "a")
print(f.write("Please append me.."))
f1.close()

writelines(lines) method

This method is used to write list of line to the file.

import os
#open myfile.txt with read mode.f is the file object
f = open("myfile.txt", "a")
f.writelines(["Line 1 ", "Line 2 ", "Line 3 "])
f.close()

Conclusion

So, in this article, you have learned all about the Python file object method with the help of examples. To know more about Python file handling, please follow out previous tutorials. Python file object methods are the most useful method when you want to work with files.

If you like this article, please share and keep visiting for further Python tutorials.

Python Function Tutorial
Python Dictionary Tutorial

Related Posts