change first line of a file in python

前端 未结 7 2075
不思量自难忘°
不思量自难忘° 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:39

    The solution i would use is to use create a file missing old first line

    from_file.readline() # and discard shutil.copyfileobj(from_file, tail_file)

    then create a file with the new first line

    then use the following to concatenate the newfirstline file and tail_file

    for f in ['newfirstline.txt','tail_file.txt']:
    with open(f,'rb') as fd:
        shutil.copyfileobj(fd, wfd, 1024*1024*10
    
    0 讨论(0)
提交回复
热议问题