Write file with the same encoding it was opened
diff --git a/codespell.py b/codespell.py
index 94e330c..99acfc6 100755
--- a/codespell.py
+++ b/codespell.py
@@ -239,6 +239,8 @@
     global encodings
     global quiet_level
 
+    encoding = encodings[0]  # if not defined, use UTF-8
+
     if filename == '-':
         f = sys.stdin
         lines = f.readlines()
@@ -276,6 +278,8 @@
                                                             file=sys.stderr)
             return
 
+        encoding = encodings[curr]
+
     i = 1
     rx = re.compile(r"[\w']+")
     for line in lines:
@@ -355,7 +359,7 @@
             if not quiet_level & QuietLevels.FIXES:
                 print("%sFIXED:%s %s" % (colors.FWORD, colors.DISABLE, filename),
                                                                 file=sys.stderr)
-            f = open(filename, 'w')
+            f = open(filename, 'w', encoding=encoding)
             f.writelines(lines)
             f.close()