]> git.phdru.name Git - dotfiles.git/blobdiff - bin/iconv.py
Feat(recode-filenames-recursive): Allow to omit parameters
[dotfiles.git] / bin / iconv.py
index 41631fea5bd449cd6128f4a33adbe5835c8bb8ac..9cb164fef0aa72bc6a2399d1ab4a988dd27d4c59 100755 (executable)
@@ -1,4 +1,4 @@
-#! /usr/bin/env python
+#! /usr/bin/env python3
 "Recode to default charset"
 
 import sys
 "Recode to default charset"
 
 import sys
@@ -10,19 +10,23 @@ to_charset = default_encoding
 
 options, arguments = getopt(sys.argv[1:], 'f:t:')
 for option, value in options:
 
 options, arguments = getopt(sys.argv[1:], 'f:t:')
 for option, value in options:
-   if option == '-f':
-      from_charset = value
-   elif option == '-t':
-      to_charset = value
+    if option == '-f':
+        from_charset = value
+    elif option == '-t':
+        to_charset = value
+
+output = getattr(sys.stdout, 'buffer', sys.stdout)
+
 
 if arguments:
 
 if arguments:
-   for file in arguments:
-      infile = open(file)
-      try:
-         for line in infile:
-            sys.stdout.write(unicode(line, from_charset, "replace").encode(to_charset, "replace"))
-      except:
-         infile.close()
+    for file in arguments:
+        with open(file, 'rb') as infile:
+            for line in infile:
+                output.write(
+                    line.decode(from_charset, "replace").
+                    encode(to_charset, "replace"))
 else:
 else:
-   for line in sys.stdin:
-      sys.stdout.write(unicode(line, from_charset, "replace").encode(to_charset, "replace"))
+    input = getattr(sys.stdin, 'buffer', sys.stdin)
+    for line in input:
+        output.write(
+            line.decode(from_charset, "replace").encode(to_charset, "replace"))