[wingide-users] Fixing line endings

Wingware Support support at wingware.com
Tue Feb 17 09:33:54 MST 2009


Michael Hipp wrote:
> I've a project that somehow ended up with a bunch of files with CRLF 
> line endings instead of my preferred LF. So I got the settings done 
> properly. But now I've edited most of the files and saved some of them 
> several times but yet it's still seemingly at random telling me about a 
> certain file having wrong line endings.
> 
> Is there no way to just force it to go check and fix all line endings in 
> all files at once rather than doing it at random as I'm editing?

Assuming you've set the Line Ending Policy in Project Properties under
the Options tab to Auto-convert on Open, you've found the right option.

However, as you noted this doesn't convert everything.  You can do that
with the Search in Files tool.  Use the Insert Carriage Return option
to the right of the Search string area, and then do Replace All with
nothing in the Replace field (which isn't shown unless you do Show
Replace from the Options menu).  Or, more accurately, replace CRLF
with LF and then any remaining CR to LF (this would work better if
the file is a strange mix of CR, CRLF, and LF).

Some care should be taken of course to make sure you don't lose data
(for example by replacing in more files than you intended).

I've also pasted in below my signature a very old Python script that
does this for all files on its command line.  This is in fact one of
the first things I ever wrote in Python, so it's pretty old/klunky
but it still works last I tried it.

-- 

Stephan Deibel
Wingware | Python IDE
Advancing Software Development

www.wingware.com

---------------------------------

#!/usr/bin/python
# Remove ^M's from all given files
#

import sys
import os
import tempfile
import string

print 'Starting conversions'

if sys.argv[-1] == '-':
   files = sys.stdin.readlines()
   def removenewline(x):
     if len(x) >2 and x[-1] == '\n' and x[-2] == '\r':
       return x[:-2]
     elif x[-1] in ('\n', '\r'):
       return x[:-1]
   files = map(removenewline, files)
else:
   files = sys.argv

first = 1
num_changed = 0
num_failed = 0
for filename in files:

   if not first:
     tempfn = tempfile.mktemp()

     print 'Checking', filename

     f1 = open(filename, 'r')
     f2 = open(tempfn, 'w')

     f1line = f1.readline()
     changed = 0
     while len(f1line) > 0:
       replace_f1line = string.replace(f1line, '\r', '')
       if replace_f1line != f1line:
         changed = 1
       f2.write(replace_f1line)
       f1line = f1.readline()

     f1.close()
     f2.close()

     if changed:
       print 'File changed; moving converted file into place'
       os.rename(filename, filename + '.old')

       try:
         if os.system('cp %s %s' % (tempfn, filename)) == 0:
           os.remove(filename + '.old')
           os.remove(tempfn)
           num_changed = num_changed + 1
         else:
           raise IOError

       # Put back old file if failed to copy into place!
       except:
         os.rename(filename + '.old', filename)
         os.remove(tempfn)
         print 'ERROR: Failed to copy file!  Old file restored'
         num_failed = num_failed + 1

   first = 0

print 'Conversions complete (%d files changed; %d failed)' % (num_changed, num_failed)




More information about the wingide-users mailing list