Skip to content

Instantly share code, notes, and snippets.

@Monal5031
Created August 1, 2018 15:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Monal5031/85b94de036369e0676e8e5b1646cef6d to your computer and use it in GitHub Desktop.
Save Monal5031/85b94de036369e0676e8e5b1646cef6d to your computer and use it in GitHub Desktop.
Python script to convert windows line ending CRLF to Unix LF.
import os
directory_in_str = os.path.dirname(os.path.realpath(__file__))
windows_line_ending = b'\r\n'
linux_line_ending = b'\n'
all_files = []
for root, dirs, files in os.walk(directory_in_str):
for name in files:
if name.endswith('.py'):
full_path = os.path.join(root, name)
if full_path.find('migrations') == -1:
all_files.append(full_path)
for filename in all_files:
print(filename)
with open(filename, 'rb') as f:
content = f.read()
content = content.replace(windows_line_ending, linux_line_ending)
with open(filename, 'wb') as f:
f.write(content)
@abarya
Copy link

abarya commented Aug 6, 2018

Consider closing the opened files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment