Python reset line in for loop

前端 未结 2 641
旧时难觅i
旧时难觅i 2021-01-27 13:02

I\'m trying to repeatedly go through a text file of a few lines. When my for loop is done with one pass through the file, I want to repeat the file from the beg

相关标签:
2条回答
  • 2021-01-27 13:39

    I gather that you want to go back to the start of the file and read it all again. The simplest way is

    infile.seek(0)

    A slower and somewhat more involved way is to close the file and reopen it.

    0 讨论(0)
  • 2021-01-27 13:56

    When you read the file, a pointer is set to the end of the file. To go back to the beginning of the file you reset the pointer by doing "infile.seek(0)" If you want to go to the end of the file you pass 2 as the argument.

    0 讨论(0)
提交回复
热议问题