# coding=utf-8 import os t = '' with open(r'E:PycharmProjectsAppHttpdataa.txt', 'rb') as lines: for line in lines: line = line.decode().replace(os.linesep, "") + ',' + os.linesep t += line b = ''.join(t.split()) with open(r'E:PycharmProjectsAppHttpdataa.txt', 'wb') as outfile: outfile.write(str.encode(b))