""" Script to remove tailing spaces in provided dir/file """

#!/usr/bin/python

import os
from sys import argv
from shutil import copy2

def trimm_file(filename):
    """ Trimms file of white spaces """
    backup = filename + ".old"
    try:
        copy2(filename, backup)
        input_file = open(backup, 'r')
        output_file = open(filename, 'w')
    except IOError:
        print("Permission denied")
        print("".join((filename, " will be ignored")))
        return

    for line in input_file:
        #\n is interpreted according to platform in text mode
        output_file.write(line.rstrip() + '\n')
    input_file.close()
    output_file.close()
    os.remove(backup)
    return

def trimmer():
    """ Run through arguments to apply trimm_file() """
    args = argv[1:]
    for arg in args:
        if os.path.isfile(arg):
            trimm_file(arg)
        elif os.path.isdir(arg):
            for path, _, files in os.walk(arg):
                for proc_file in files:
                    _, file_extension = os.path.splitext(proc_file)
                    if not file_extension == '':
                        trimm_file(os.path.join(path, proc_file))
        else:
            print("".join((arg, " is not found")))

if __name__ == "__main__":
    trimmer()

@@ -84,16 +84,86 @@
 port os%0A
+#use io variant of open(). It has encoding in py2%0Afrom io import open%0A
 from sys
@@ -376,16 +376,34 @@
 kup, 'r'
+, encoding='utf-8'
 )%0A      
@@ -436,16 +436,34 @@
 ame, 'w'
+, encoding='utf-8'
 )%0A    ex
@@ -581,24 +581,81 @@
  return%0A%0A   
+ try:%0A        for line in (%22%22.join((line.rstrip(), '%5Cn'))
  for line in
@@ -665,16 +665,17 @@
 put_file
+)
 :%0A      
@@ -680,110 +680,181 @@
     
-#%5Cn is interpreted ac
+    output_file.write(line)%0A    except UnicodeDecodeError as e:%0A        print(%22%22.join((%22En
 co
-r
 ding 
-to platform in text mode%0A        output_file.write(line.rstrip() + '%5Cn'
+error: %22, str(e))))%0A        print(%22%22.join((%22Failed to trimm %22, filename))
 )%0A
+%0A
     
@@ -1291,16 +1291,16 @@
 c_file)%0A
-
         
@@ -1338,16 +1338,49 @@
 on == ''
+ and not file_extension == %22.old%22
 :%0A      

Improve error handling