Problem Description
Write a python program to create a file and display the contents (dynamic number of lines)
Hint:
1. Create a File
2. Take the input as number of lines to be written into the file
3. Based on the number of lines get the input from the user
4. Add a new line to the file after each input.
5. Display the contents of the file written in the output
6. Display the Number of Lines in the File in the output
Input:
1. Filename
2,3,4,5 = File contentsCODING ARENA
import os
x=input()
y=int(input())
count=0
f = open(x,"w")
for i in range(y):
a=input()
f.write(a + os.linesep)
f.close()
for line in (open(x)):
print(line.rstrip())
print()
count+=1
print("Number of lines:")
print(count)
Case 1
Input (stdin)sample.txt 5 This is First Line This is Second Line This is Third Line This is Fourth Line This is Fifth Line
Expected OutputThis is First Line This is Second Line This is Third Line This is Fourth Line This is Fifth Line Number of lines: 5
Test Case 2
Input (stdin)sample.txt 4 This is First Line This is Second Line This is Third Line This is Fourth Line
Expected OutputThis is First Line This is Second Line This is Third Line This is Fourth Line Number of lines: 4
No comments:
Post a Comment