how to output every line in a file python

后端 未结 5 2144
执笔经年
执笔经年 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:44

    You probably want something like:

    if data.find('!masters') != -1:
         f = open('masters.txt')
         lines = f.read().splitlines()
         f.close()
         for line in lines:
             print line
             sck.send('PRIVMSG ' + chan + " " + str(line) + '\r\n')
    

    Don't close it every iteration of the loop and print line instead of lines. Also use readlines to get all the lines.

    EDIT removed my other answer - the other one in this discussion is a better alternative than what I had, so there's no reason to copy it.

    Also stripped off the \n with read().splitlines()

提交回复
热议问题