__author__ = 'L'
import sys
import os
import stat
def change_dos_to_unix(path):
for item in os.listdir(path):
subpath = os.path.join(path, item)
mode = os.stat(subpath)[stat.ST_MODE]
if stat.S_ISDIR(mode):
continue
else:
with open(subpath, "rb+") as file_handle:
all_text = file_handle.read()
all_change_text = all_text.replace(b"\r\n", b"\n")
print(len(all_change_text))
file_handle.seek(0, 0)
file_handle.truncate()
file_handle.write(all_change_text)
print("change %s file to unix format." % subpath)
def change_unix_to_dos(path):
for item in os.listdir(path):
subpath = os.path.join(path, item)
mode = os.stat(subpath)[stat.ST_MODE]
if stat.S_ISDIR(mode):
continue
else:
with open(subpath, "rb+") as file_handle:
all_text = file_handle.read()
all_change_text = all_text.replace(b"\n", b"\r\n")
file_handle.seek(0, 0)
file_handle.truncate()
file_handle.write(all_change_text)
print("change %s file to dos format." % subpath)
if len(sys.argv) >= 2:
current_path = sys.argv[1]
if len(sys.argv) == 3:
change_unix_to_dos(current_path)
else:
change_dos_to_unix(current_path)