Created
March 7, 2011 08:58
-
-
Save tanakahisateru/858250 to your computer and use it in GitHub Desktop.
Convert CRLF to LF in a folder recursively.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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