Using “readlines()” twice in a row

后端 未结 3 532
旧时难觅i
旧时难觅i 2020-11-30 15:22

I\'m trying to do something like this:

Lines = file.readlines()
# do something
Lines = file.readlines()  

but the second time Lines

3条回答
  •  星月不相逢
    2020-11-30 16:02

    In order to not have to reset every time by using seek method again and again, use the readlines method, but you must store it in variable like this example below:

    %%writefile test.txt
    this is a test file!
    #open it
    op_file = open('test.txt')
    #read the file
    re_file = op_file.readlines()
    re_file
    #output
    ['this is a test file!']
    # the output still the same
    re_file
    ['this is a test file!']
    

提交回复
热议问题