Read only the first line of a file?

前端 未结 8 1128
死守一世寂寞
死守一世寂寞 2021-01-29 21:29

How would you get only the first line of a file as a string with Python?

8条回答
  •  长发绾君心
    2021-01-29 22:27

    To go back to the beginning of an open file and then return the first line, do this:

    my_file.seek(0)
    first_line = my_file.readline()
    

提交回复
热议问题