0

There is already a similar question, but I would like to use python, not Vim.

I want to remove single line breaks but to keep empty lines,

This is a test. 
This 
is a test.

This is a test. 
This is a 
test.

I would like to transform it into:

This is a test. This is a test

This is a test. This is a test

I am using a code to replace texts, I would like to know if it is possible to adapt it...

import os
texttofind = ''
texttoreplace = ''
sourcepath = os.listdir('InputFiles/')

for file in sourcepath:
    inputfile = 'InputFiles/' + file
    print('Conversion is ongoing for:' + inputfile)
    with open(inputfile, 'r', encoding='utf-8') as inputfile:
        filedata = inputfile.read()
        freq = 0
        freq = filedata.count(texttofind)
    destinationpath = 'OutputFile/' + file
    filedata = filedata.replace(texttofind, texttoreplace)
    with open(destinationpath, 'w', encoding='utf-8') as file:
        file.write(filedata)
    print('Total %d Record Replaced' % freq)

If i use texttofind = '\n' the empty lines will also be replaced... Maybe a modification in the for to ignore empty lines, but I don't know how to do that, could anyone help?

Mrcrg
  • 101
  • 1

0 Answers0