Home > OS >  Outputting a specific line from a list in a file and converting it into a string
Outputting a specific line from a list in a file and converting it into a string

Time:12-09

I am stuck on a task of where I have to output n-th line from a file and I need it outputted as a string.

file = open("/usercode/files/pull_ups.txt")
n = int(input())
print(file.readlines(n))
file.close()`enter code here

This outputs '['Day 0, 8 pull ups\n']' and I need it to output Day 1, 8 pull ups

CodePudding user response:

with open("file.txt") as file: # cleaner way of opening a file
   n = int(input())
   for i in range(n-1):        # skip first n-1 lines of a file
      file.readline()
   print(file.readline())      # read n-th line
  • Related