how to output every line in a file python

后端 未结 5 2142
执笔经年
执笔经年 2021-01-04 04:01
     if data.find(\'!masters\') != -1:
         f = open(\'masters.txt\')
         lines = f.readline()
         for line in lines:
               print lines
               


        
5条回答
  •  青春惊慌失措
    2021-01-04 04:37

    You could try this. It doesn't read all of f into memory at once (using the file object's iterator) and it closes the file when the code leaves the with block.

    if data.find('!masters') != -1:
        with open('masters.txt', 'r') as f:
            for line in f:
                print line
                sck.send('PRIVMSG ' + chan + " " + line + '\r\n')
    

    If you're using an older version of python (pre 2.6) you'll have to have

    from __future__ import with_statement
    

提交回复
热议问题