From: Oleg Broytman Date: Thu, 13 Mar 2014 21:29:06 +0000 (+0400) Subject: Create the directory for the output files X-Git-Tag: v2.5.0~12 X-Git-Url: https://git.phdru.name/?p=mimedecode.git;a=commitdiff_plain;h=41e92ab685cbcf842d1c4f33b8b29f94193938ed Create the directory for the output files Create the directory for the output files (-O) if it doesn't exits. --- diff --git a/ANNOUNCE b/ANNOUNCE index 201eccd..5bd1a55 100644 --- a/ANNOUNCE +++ b/ANNOUNCE @@ -38,6 +38,8 @@ headers/bodies/messages to files. Add option -O to set the destination directory for output files. + Create the directory for the output files (-O) if it doesn't exits. + Fix a minor bug: if a multipart message (or a subpart) lacks any textual content - avoid putting an excessive newline. diff --git a/mimedecode.docbook b/mimedecode.docbook index 9acc320..58ca08d 100644 --- a/mimedecode.docbook +++ b/mimedecode.docbook @@ -468,8 +468,8 @@ -O dest_dir - Set destination directory for the output files; the directory must - exist. Default is current directory. + Set destination directory for the output files; if the directory + doesn't exist it will be created. Default is current directory. diff --git a/mimedecode.py b/mimedecode.py index 392e707..c1731d6 100755 --- a/mimedecode.py +++ b/mimedecode.py @@ -396,7 +396,16 @@ def decode_message(msg): def open_output_file(filename): - return open(os.path.join(g.destination_dir, filename), 'w') + fullpath = os.path.abspath(os.path.join(g.destination_dir, filename)) + full_dir = os.path.dirname(fullpath) + create = not os.path.isdir(full_dir) + if create: + os.makedirs(full_dir) + try: + return open(fullpath, 'w') + except: + if create: + os.removedirs(full_dir) class GlobalOptions: