Python readlines not returning anything?

前端 未结 2 846
挽巷
挽巷 2020-12-06 15:01

I have the following code:

with open(\'current.cfg\', \'r\') as current:
    if len(current.read()) == 0:
        print(\'FILE IS EMPTY\')
    else:
                 


        
2条回答
  •  半阙折子戏
    2020-12-06 15:32

    When you do current.read(), you consume the contents of the file, so a subsequent current.readlines() returns an empty list.

    Martijn Pieters's code is the way to go.

    Alternatively, you could do rewind to the beginning of the file with a current.seek(0) before the readlines(), but this is unnecessarily complicated.

提交回复
热议问题