Write a program in Python which allows you to insert at the 3rd position of an existing file called my File.txt, the line "This line was inserted via Python code! " without changing the existing content
file.
f = open('myFile.txt', 'r')
s = f.readlines()
f.close()
s.insert(2, 'This line was inserted via Python code!\n')
f = open('myFile.txt', 'w')
f.write(''.join(s))
Comments
Leave a comment