# coding=utf-8 import os t = '' with open(r'E:\PycharmProjects\AppHttp\data\a.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:\PycharmProjects\AppHttp\data\a.txt', 'wb') as outfile: outfile.write(str.encode(b))