change first line of a file in python

前端 未结 7 2105
不思量自难忘°
不思量自难忘° 2020-12-02 19:05

I only need to read the first line of a huge file and change it.

Is there a trick to only change the first line of a file and save it as another file using Python? A

7条回答
  •  無奈伤痛
    2020-12-02 19:15

    The sh module worked for me:

    import sh
    
    first = "new string"
    sh.sed("-i", "1s/.*/" + first + "/", "file.x")
    

提交回复
热议问题