Python File methods (With Useful Examples)

In this Python tutorial, we will discuss file methods in Python and also we will check the below topics.

  • Python file write() method
  • Python file close() method
  • Python file read() method
  • Python file readline() method
  • Python file writable() method
  • Python file readable() method
  • Python file fileno() method
  • Python file truncate() method
  • Python file flush() method
  • Python file tell() method

We will see all the Python File methods with various examples.

If you are new to Python, you can check out an article to create your first hello world program in Python.

Below are the list of Python file methods.

Python file write() method

  • Python file write() method is used to write the specified text to the file. Here, we opened the file with “a” which is used for appending, and then some text is added to the file.
  • If we will use “a” the text will be added to the current file, by default at the end of the file.
  • f.write(” Happy Learning! “) is appended on the file at the last.

Example:

f = open("examplefile.txt", "a")
f.write(" Happy Learning! ")
f.close()
f = open("examplefile.txt", "r")
print(f.read())

After writing the above code, Once you will print then the output will appear as a “ Hello all! Welcome to file examples. Good luck! Happy Learning! ”. Here, we opened the file “examplefile.txt” in “r” mode so, it will get opened and we will able to read the file after appending.

You can refer to the below screenshot for the output.

Python file write() method
Python file write() method

Python file close() method

The file close() method in Python is used for closing the opened file. It is a good practice to close the file using close() method when it is opened.

Example:

f = open("examplefile.txt", "r")
print(f.read())
f.close()

Here, we have opened the file as “examplefile.txt” in the “r” mode and at last, we have used the close() method for closing the file.

When we run the above program, it produces the following output.

Python file close() method
Python file close() method

Python file read() method

The read() method in Python file operations is used for reading the content of the file from the “examplefile.txt”. The read() method return the specified bytes from the file and by default it is -1 which mean it will read the whole file content.

Example:

f = open("examplefile.txt", "r"
print(f.read())

After writing the above code, when we will print “f.read()” then the output will appear “Hello all! Welcome to file examples. Happy reading!”. Here, as we did not mention the specified number of bytes. So, the read() method returns the whole file by default.

You can refer to the below screenshot for python file read() method.

Python file read() method
Python file read() method

Python file readline() method

The Python file readline() method reads one entire first line from the “examplefile.txt” file. By using the size parameter we can get the specified bytes in output.

Example:

f = open("examplefile.txt", "r")
print(f.readline())

Here, when we will print “f.readline()” then the output will appear “Hello all! Welcome to file examples.”. Also, we can see that the readline() returns the first line from the file. We can also specify how many bytes from the line to return.

You can refer to the below screenshot for python file read() method.

Python file readline() method
Python file readline() method

Python file writable() method

The Python file writable() method is an inbuilt method that returns True if the file is writable otherwise it will return False if the file is not writable. A file is writable only if it’s opened using “w” for write and “a” for append.

Example:

myfile = open("examplefile.txt", "w")
print(myfile.writable())
myfile.close()

To get the output we will print “myfile.writable()” it will return the boolean value “True” which means the file is writable and the file is opened using “w”.

The Below screenshot shows the output for file writable() method in python.

Python file writable() method
Python file writable() method

Python file readable() method

The readable() method in Python is an inbuilt method in python, it checks whether a file is readable or not. This method returns a boolean value. The readable() method returns True if the file is readable otherwise it will return False.

Example:

myfile = open("examplefile.txt", "r")
print(myfile.readable())
myfile.close()

To get the output we will print “myfile.readable()” and it will return “True” as an output because the file is readable. A file is readable if it’s opened using “r”.

When we run the above program, it produces the following output.

Python file readable() method
Python file readable() method

Python file fileno() method

The Python file fileno() method is an inbuilt method in python, which returns the file descriptor of the stream as a number. An error can occur if the operating system does not use a file descriptor.

Example:

myfile = open("example.txt", "r")
print(myfile.fileno())

Here, when we will print “myfile.fileno()” then the output will appear “ 3 ”. Also, we can see that the fileno() returns an integer value which is the file descriptor.

You can refer to the below screenshot for python file fileno() method.

Python file fileno() method
Python file fileno() method

Python file truncate() method

  • The truncate() method in Python file, is an inbuilt method in python, it resizes the file to a given number of bytes.
  • Firstly, I have opened the file in “w” mode. And then we write on the file. Here, I have given the size as “25” which is an optional parameter.
  • After the truncate method, we will read the file, and print “myfile.read()” to see the output.

Example:

myfile = open("examplefile.txt", "w")
myfile.write("Python is one of the good programming language")
myfile.truncate(25)
myfile.close()
myfile = open("examplefile.txt", "r")
print(myfile.read())

To get the output, we will print “myfile.read()” then the output will appear “ Python is one of the good ”. So, it will truncate the file based on the given size which is “25”.

You can refer to the below screenshot for python file truncate() method.

Python file truncate() method
Python file truncate() method

Python file flush() method

The flush() method is an inbuilt method in python. It is used to clear the internal buffer when writing to file. It is the best method while working with file handling in python.

Example:

myfile = open("examplefile.txt", "a")
myfile.write("Welcome to python")
myfile.flush()
myfile.write("Programming.....")

To see the output, we will use “myfile.flush()” and it will clear the internal buffer of the file. But, it does not affect the content of the file. So, the content of the file can be read and displayed.

The Below screenshot shows the output for the file flush() method in python, and it returns nothing on the output.

Python file flush() method
Python file flush() method

Python file tell() method

Python file tell() method returns the current file position within the file. It does not take any parameter and returns an integer value.

Example:

myfile = open("examplefile.txt", "r")
print(myfile.tell())
myfile.close()

Here, when we will print “myfile.tell()” then the output will appear “ 0 ”. Here, initially file pointer points to the beginning of the file if not opened in append mode. So, the initial value of tell() is zero.

You can refer to the below screenshot for python file tell() method.

Python file tell() method
Python file tell() method

You may like the following Python tutorials:

In this Python tutorial, we have learned about the File methods in python with a few examples. Also, We covered these below topics:

  • Python file write() method
  • Python file close() method
  • Python file read() method
  • Python file readline() method
  • Python file writable() method
  • Python file readable() method
  • Python file fileno() method
  • Python file truncate() method
  • Python file flush() method
  • Python file tell() method