handle error when reading templates
authorJean-Philippe Orsini <jeanfi@gmail.com>
Mon, 1 Feb 2016 14:57:25 +0000 (15:57 +0100)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Mon, 1 Feb 2016 14:57:25 +0000 (15:57 +0100)
mp3tohtml/mp3tohtml.py

index ee51dee..4a9d8a3 100755 (executable)
@@ -50,15 +50,30 @@ def fma_copyright_to_html(path, copyright):
 
 def copy_header(path):
     header_path = path + "/header.tpl"
 
 def copy_header(path):
     header_path = path + "/header.tpl"
-    header = open(header_path, "r")
-    for line in header:
-        sys.stdout.write(line)
+    try:
+        header = open(header_path, "r")
+        for line in header:
+            sys.stdout.write(line)
+    except IOError as err:
+        sys.stderr.write("Failed to read header file "
+                         + header_path
+                         + ": " + err.strerror + "\n")
+        sys.exit(1)
+    header.close()
 
 def copy_footer(path):
     footer_path = path + "/footer.tpl"
 
 def copy_footer(path):
     footer_path = path + "/footer.tpl"
-    footer = open(footer_path, "r")
-    for line in footer:
-        sys.stdout.write(line)
+    try:
+        footer = open(footer_path, "r")
+        for line in footer:
+            sys.stdout.write(line)
+    except IOError as err:
+        sys.stderr.write("Failed to read footer file "
+                         + footer_path
+                         + ": " + err.strerror + "\n")
+        sys.exit(1)
+    footer.close()
+
 
 parser = argparse.ArgumentParser(description='Generate an HTML containing information about the MP3 files.')
 parser.add_argument('dir', help='The directory containing the MP3 files')
 
 parser = argparse.ArgumentParser(description='Generate an HTML containing information about the MP3 files.')
 parser.add_argument('dir', help='The directory containing the MP3 files')