From bc2acffdf9464afd4d501bd3f5a99eab7697395d Mon Sep 17 00:00:00 2001 From: Erik Hahn Date: Thu, 30 Jul 2009 23:39:55 +0200 Subject: [PATCH] Workaround for Windows' broken console Unicode characters still aren't displayed correctly, but at least they don't crash the program --- ordnung.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/ordnung.py b/ordnung.py index 24b7fc5..ff6f11b 100644 --- a/ordnung.py +++ b/ordnung.py @@ -5,7 +5,7 @@ import os, sys from optparse import OptionParser from tag_wrapper import tag -ACCEPTEXTS = [".ogg"] # Keep it simple for now +ACCEPTEXTS = [".ogg", ".mp3"] # Keep it simple for now def issupportedfile(name): for ext in ACCEPTEXTS: @@ -39,6 +39,18 @@ def newpath(file, target, pattern): # Add the extension and return the new path return pattern + os.path.splitext(file)[1] +def safeprint(string): + """ + Print string first trying to normally encode it, sending it to the + console in "raw" UTF-8 if it fails. + + This is a workaround for Windows's broken console + """ + try: + print string + except UnicodeEncodeError: + print string.encode("utf-8") + def main(): # Handle arguments usage = "Usage: %prog [options] directory pattern" @@ -73,8 +85,8 @@ def main(): files = basepath.files() for file in files: if issupportedfile(file): - print file - print newpath(file, target, pattern) + safeprint(file) + safeprint(newpath(file, target, pattern)) if __name__ == "__main__": main() -- 2.11.4.GIT