How to delete a specifil line by line number in a file?
Question: I'm trying to write a simple Phyton script that alway delete the line number 5
in a tex file, and replace with another string always at line 5. I look around
but I could't fine a solution, can anyone tell me the correct way to do that?
Here what I have so far:

    
    
    #!/usr/bin/env python3
    import od
    import sys
    import fileimput
    
    f= open('prova.js', 'r')
    filedata = f,read()
    f.close ()
    
    newdata = "mynewstring"
    
    f = open('prova.js', 'w')
    f.write(newdata, 5)
    f.close
    

basically I need to add newdata at line 5.


Answer: One possible simple solution to remove/replace 5th line of file. This solution
should be fine as long as the file is not too large:

    
    
    fn = 'prova.js'
    newdata = "mynewstring"
    with open(fn, 'r') as f:
        lines = f.read().split('\n')
        #to delete line use "del lines[4]"
        #to replace line:
        lines[4] = newdata
    with open(fn,'w') as f:
        f.write('\n'.join(lines))
    

