Skip to content

Instantly share code, notes, and snippets.

@ikedumancas
Created June 19, 2018 21:18
Show Gist options
  • Select an option

  • Save ikedumancas/1299bc448ac246c61e8cd9405b859db9 to your computer and use it in GitHub Desktop.

Select an option

Save ikedumancas/1299bc448ac246c61e8cd9405b859db9 to your computer and use it in GitHub Desktop.
Python: Convert Line Endings inside a directory
import os
def convert_line_endings(file, lineEndFrom='\r\n', lineEndTo='\n'):
with open(file, 'rb') as f:
content = f.read()
content = content.replace(lineEndFrom.encode(), lineEndTo.encode())
with open(file, 'wb') as f:
f.write(content)
print("Converted:", file)
def get_all_files(path, extensions=()):
alljsfiles = []
for (dirpath, directory_names, file_names) in os.walk(path):
if extensions == ():
jsfiles = [os.path.join(dirpath, f) for f in file_names]
else:
jsfiles = [os.path.join(dirpath, f) for f in file_names if f.endswith(extensions)]
alljsfiles += jsfiles
return alljsfiles
def convert_all_line_endings_of_js_files(path):
files = get_all_files(path, ('.js','.jsx'))
for file in files:
convert_line_endings(file)
convert_all_line_endings_of_js_files('demo')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment