Index: django/core/management/commands/makemessages.py
===================================================================
--- django/core/management/commands/makemessages.py	(revision 8534)
+++ django/core/management/commands/makemessages.py	(working copy)
@@ -106,16 +106,19 @@
                 msgs = stdout.read()
                 errors = stderr.read()
                 if errors:
+                    os.unlink(os.path.join(dirpath, thefile))
+                    if os.path.exists(potfile):
+                        os.unlink(potfile)
                     raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))
-                old = '#: '+os.path.join(dirpath, thefile)[2:]
-                new = '#: '+os.path.join(dirpath, file)[2:]
-                msgs = msgs.replace(old, new)
-                if os.path.exists(potfile):
-                    # Strip the header
-                    msgs = '\n'.join(dropwhile(len, msgs.split('\n')))
-                else:
-                    msgs = msgs.replace('charset=CHARSET', 'charset=UTF-8')
                 if msgs:
+                    old = '#: '+os.path.join(dirpath, thefile)[2:]
+                    new = '#: '+os.path.join(dirpath, file)[2:]
+                    msgs = msgs.replace(old, new)
+                    if os.path.exists(potfile):
+                        # Strip the header
+                        msgs = '\n'.join(dropwhile(len, msgs.split('\n')))
+                    else:
+                        msgs = msgs.replace('charset=CHARSET', 'charset=UTF-8')
                     open(potfile, 'ab').write(msgs)
                 os.unlink(os.path.join(dirpath, thefile))
             elif domain == 'django' and (file_ext == '.py' or file_ext in extensions):
@@ -132,17 +135,21 @@
                 msgs = stdout.read()
                 errors = stderr.read()
                 if errors:
+                    if thefile != file:
+                        os.unlink(os.path.join(dirpath, thefile))
+                    if os.path.exists(potfile):
+                        os.unlink(potfile)
                     raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))
-                if thefile != file:
-                    old = '#: '+os.path.join(dirpath, thefile)[2:]
-                    new = '#: '+os.path.join(dirpath, file)[2:]
-                    msgs = msgs.replace(old, new)
-                if os.path.exists(potfile):
-                    # Strip the header
-                    msgs = '\n'.join(dropwhile(len, msgs.split('\n')))
-                else:
-                    msgs = msgs.replace('charset=CHARSET', 'charset=UTF-8')
                 if msgs:
+                    if thefile != file:
+                        old = '#: '+os.path.join(dirpath, thefile)[2:]
+                        new = '#: '+os.path.join(dirpath, file)[2:]
+                        msgs = msgs.replace(old, new)
+                    if os.path.exists(potfile):
+                        # Strip the header
+                        msgs = '\n'.join(dropwhile(len, msgs.split('\n')))
+                    else:
+                        msgs = msgs.replace('charset=CHARSET', 'charset=UTF-8')
                     open(potfile, 'ab').write(msgs)
                 if thefile != file:
                     os.unlink(os.path.join(dirpath, thefile))
@@ -152,13 +159,15 @@
             msgs = stdout.read()
             errors = stderr.read()
             if errors:
+                os.unlink(potfile)
                 raise CommandError("errors happened while running msguniq\n%s" % errors)
-            open(potfile, 'w').write(msgs)
             if os.path.exists(pofile):
+                open(potfile, 'w').write(msgs)
                 (stdin, stdout, stderr) = os.popen3('msgmerge -q "%s" "%s"' % (pofile, potfile), 'b')
                 msgs = stdout.read()
                 errors = stderr.read()
                 if errors:
+                    os.unlink(potfile)
                     raise CommandError("errors happened while running msgmerge\n%s" % errors)
             open(pofile, 'wb').write(msgs)
             os.unlink(potfile)
