Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - READING FILES


So, in the previous tutorial, we have created a file named 'myfirstfile.txt', with the below contents,


java_Collections

Now, let us see, how can we read the above file in Python.


How to read the entire file?


We can use the 'read( )' Function to read the total file.


There is just one mode for reading a file :


  1. "r" - Read Mode - This is the default mode. Used to opens a file for reading and throws an error if the file is not present.

Example :



myfile = open("myfirstfile.txt", "r")
str = myfile.read()
print(str)
myfile.close()


Output :



  In a huge pond,
  there lived many fish.
  They were arrogant and
  never listened to anyone.
  In this pond,
  there also lived
  a kind-hearted crocodile.

  1. So, what we have done in the above example is, opened the file in read Mode(i.e. 'r').

    myfile = open("myfirstfile.txt", "r")


    We haven't specified the 'Text Mode' (i.e. 't') because it is the default mode.
  2. Then we have used the 'read( )' Function to read the entire contents of the file.

    str = myfile.read( )


    And initialised the contents of the file in a variable, 'str' as a String.

    java_Collections
  3. After that we have used the print statement to print the contents of 'str'(The string that contains the contents of the file).

    print(str)
  4. Finally, we close the file using the 'close( )' Function.

    myfile.close( )

How to read only a single line in the File?


We can use the 'readline( )' Function to read the first line of the file.


Example :



myfile = open("myfirstfile.txt", "r")
str = myfile.readline()
print(str)
myfile.close()


Output :



  In a huge pond,

So, in the above example, we have used the 'readline( )' Function to read the first line of the file.


str = myfile.readline( )

Rest of the lines are just the same.


How to read the file by specifying the number of characters?


We can use the number of character with the 'read( )' Function to read the File by number of characters.


Example :



myfile = open("myfirstfile.txt", "r")
str = myfile.read(9)
print(str)
myfile.close()


Output :



  In a huge

So, in the above example, we have used the 'read( )' Function and specified '9' as argument to read the first 9 characters of the file.


str = myfile.read(9)

And got the below output.


In a huge

How to get the current position of the file pointer?


In the above example, we have used the 'read( )' Function and specified '9' as argument to read the first 9 characters of the file.


Now, the file pointer should be at '9'.


And we can get the current file pointer using the 'tell( )' Function.


Example :



myfile = open("myfirstfile.txt", "r")
str = myfile.read(9)
print(myfile.tell())
myfile.close()    


Output :



  9

And all we have done is used the 'tell( )' Function to get the position of the file pointer.


print(myfile.tell( ))

And as we know the current file poin ter is at 9. So we got 9 as output.


How to change the current position of the file pointer?


In the above example, we have used the 'tell( )' Function to identify the current file pointer position.


Now, what if we want to change the file pointer, so that it points to some other location.


And we can achieve it using the 'seek( )' Function.


Let us see in the below example.


Example :



myfile = open("myfirstfile.txt", "r")
str = myfile.read(9)
print(myfile.tell())
myfile.seek(0)
print(myfile.tell())
myfile.close() 


Output :



  9
  0

So, if we see the output, we can see that the 'tell( )' Function is pointing to position 9.


print(myfile.tell( ))

Then we have used the 'seek( )' Function to reset the position of the pointer to '0'.


myfile.seek(0)

And if you see the next 'tell( )' Function,


print(myfile.tell( ))

It points to location '0'.