Quantcast
Channel: Replace newlines with literal \n - Stack Overflow
Viewing all articles
Browse latest Browse all 8

Answer by voltento for Replace newlines with literal \n

$
0
0

Here is little python script for replacing the '\r\n' with '\r' in directory in a recursive way import os import sys

if len(sys.argv) < 2:    print("Wrong arguments. Expected path to directory as arg 1.")    exit(1)path = sys.argv[1]def RecOpOnDir(path, op) :    for f in os.listdir(path):        full_f = path +"/"+ f        if os.path.isdir(full_f):            RecOpOnDir(full_f, op)        else:            try:                op(full_f)            except Exception as ex:                print("Exception during proc '", full_f, "' Exception:", ex)file_counter = 0def WinEndingToUnix(path_to_file):    global file_counter    file_counter += 1    file_strs = []    with open(path_to_file) as f:        for line in f:            file_strs.append(line.replace(r"\r\n", r"\n"))    with open(path_to_file, "w") as fw:        fw.writelines(l for l in file_strs)try:    RecOpOnDir(path, WinEndingToUnix)    print("Completed.", file_counter, "files was reformed")except Exception as ex:    print("Exception occured: ", ex)    exit(1)

Viewing all articles
Browse latest Browse all 8

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>