Working with readline(), readlines() functions, closed keyword in Python

  • readline() returns only one line of the file if not provided with any argument.
  • readline() takes only one integer argument and it will print the that number of characters that are passed in the arguments.
  • readlines() returns all the lines of the file in the form of a list.
  • ‘close’ checks whether a file is closed or not.
  • ‘close’ returns a boolean value either True if the file is closed or False if the file is not closed.
f = open('file1.txt', 'r')

print(f.readline(), end = "")

lines = f.readlines()

for line in lines:
    print(line, end = "")

print(" ")

print(f.closed)

f.close()

print(f.closed)

OUTPUT :

Hey, I'm Sayan Dey.
I'm from Chandannagar.
My emai id is - d.sayan1998@gamil.com.
There's a big pandemic situation in the world. Hope everyone is okay.
Stay at home.
Stay safe.
Use sanitizers.
Use hand gloves.
Use handwashes. 
False
True

CONTENT OF MY FILE :

Hey, I’m Sayan Dey.
I’m from Chandannagar.
My emai id is – d.sayan1998@gamil.com.
There’s a big pandemic situation in the world. Hope everyone is okay.
Stay at home.
Stay safe.
Use sanitizers.
Use hand gloves.
Use handwashes.