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
Input:
1. Filename
2,3,4,5 = File contentsCODING ARENA
import os
fname = input()
count=0
with open(fname, 'w+') as f:
a=int(input())
for i in range(a):
x=input()
f.write(x+ os.linesep)
f.write("\n")
f.close()
for line in (open(fname)):
print(line.rstrip())
print()
count+=1
f.close()
print("Number of lines:")
print(count)
Test 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: 10
Test Case 2
Input (stdin)sample.txt 3 This is First Line This is Second Line This is Third Line
Expected OutputThis is First Line This is Second Line This is Third Line Number of lines: 6
No comments:
Post a Comment