How to read external file in python?

Three methods to read a file

  1. read() It returns the read bytes in form of a string.

    fileObject.read()

You can also define, how many bytes to read by

`fileObject.read([n]) //If n is not define, it will read the whole file`
  1. readline([n]) It reads the line and return in the form of string, It only read single line

    fileObject.readline([n])

  2. readlines() It reads all the lines of the file and return each line a string element in a list

    fileObject.readlines()

Hope it helps

Leave a Comment