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.

Opening, reading contents, seeking cursor position in a file, tell function using Python

  • open() function opens the file and returns a file object.
  • tell() function tells the current cursor position.
  • seek() function allows us to change the cursor position.
  • close() function closes the file.
  • The program will also run perfectly if we don’t close a file. But closing a file is a good practice.
f = open('File1.txt')
print(f"Curser position is - {f.tell()}")
print(f.read())
print(f"Curser position is - {f.tell()}")
print(f"Before seek method, cursor position was at - {f.tell()}")
f.seek(0)
print(f"After seek method, cursor position is at - {f.tell()}")
print(f.read())
f.close()

OUTPUT :

Curser position is - 0
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.
Curser position is - 233
Before seek method, cursor position was at - 233
After seek method, cursor position is at - 0
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.

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.

Debugging in Python

  • l – Basically a pointer that points which line is current being executed.
  • n – Executes the line.
  • c – Continues the code without further debugging.
  • q – Quits debugging.
import pdb

pdb.set_trace()
name = input("Enter your name : ")
age = input("Enter your age : ")
print(f"You have entered {name} and {age}")
age = age + 5
print("You'll be {age} in the after 5 years")





P.S : THIS CODE IS WRONG. THIS CODE IS INTENTIONALLY WRITTEN WRONG TO DEBUG IT. DON'T COPY. THE CORRECT CODE IS PROVIDED BELOW THE OUTPUT.

OUTPUT :

> c:\users\dsaya\onedrive\desktop\pythonpractise\oop\debuggin_in_python.py(4)<module>()
-> name = input("Enter your name : ")
(Pdb) l
  1     import pdb
  2
  3     pdb.set_trace()
  4  -> name = input("Enter your name : ")
  5     age = input("Enter your age : ")
  6     print(f"You have entered {name} and {age}")  
  7     age = age + 5
  8     print("You'll be {age} in the after 5 years")
[EOF]
(Pdb) n
Enter your name : Sayan
> c:\users\dsaya\onedrive\desktop\pythonpractise\oop\debuggin_in_python.py(5)<module>()
-> age = input("Enter your age : ")
(Pdb) n
Enter your age : 22
> c:\users\dsaya\onedrive\desktop\pythonpractise\oop\debuggin_in_python.py(6)<module>()
-> print(f"You have entered {name} and {age}")
(Pdb) n
You have entered Sayan and 22
> c:\users\dsaya\onedrive\desktop\pythonpractise\oop\debuggin_in_python.py(7)<module>()
-> age = age + 5
(Pdb) n
TypeError: can only concatenate str (not "int") to str
> c:\users\dsaya\onedrive\desktop\pythonpractise\oop\debuggin_in_python.py(7)<module>()
-> age = age + 5
(Pdb) q
Traceback (most recent call last):
  File "c:/Users/dsaya/OneDrive/Desktop/PythonPractise/OOP/Debuggin_In_Python.py", line 7, in <module>
    age = int(age) + 5
  File "C:\Python385\lib\bdb.py", line 94, in trace_dispatch
    return self.dispatch_exception(frame, arg)
  File "C:\Python385\lib\bdb.py", line 174, in dispatch_exception
    if self.quitting: raise BdbQuit
bdb.BdbQuit

CORRECT CODE :

import pdb

pdb.set_trace()
name = input("Enter your name : ")
age = input("Enter your age : ")
print(f"You have entered {name} and {age}")
age = age + 5
print("You'll be {age} in the after 5 years")