Answer to Question #231123 in Python for Rachna Pal

Question #231123

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.


1
Expert's answer
2021-09-02T12:39:01-0400
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))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS