Skip to content

Instantly share code, notes, and snippets.

@tanakahisateru
Created March 7, 2011 08:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanakahisateru/858250 to your computer and use it in GitHub Desktop.
Save tanakahisateru/858250 to your computer and use it in GitHub Desktop.
Convert CRLF to LF in a folder recursively.
# Convert CRLF to LF in a folder recursively.
import sys, os
if len(sys.argv) < 2:
raise RuntimeError("No folder specified to convert.")
def iterfiles(basedir):
for (path, dirs, files) in os.walk(basedir):
for fn in files:
yield os.path.join(path, fn)
def trim_cr(line):
if line[-1] == "\n":
return line.rstrip("\r\n") + "\n"
else:
return line
for fpath in iterfiles(sys.argv[1]):
print fpath
with open(fpath) as f:
txt = "".join(map(trim_cr, f))
with open(fpath, 'wb') as f: # 'b' for windows stdio
f.write(txt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment