From 41e92ab685cbcf842d1c4f33b8b29f94193938ed Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Fri, 14 Mar 2014 01:29:06 +0400 Subject: [PATCH] Create the directory for the output files Create the directory for the output files (-O) if it doesn't exits. --- ANNOUNCE | 2 ++ mimedecode.docbook | 4 ++-- mimedecode.py | 11 ++++++++++- 3 files changed, 14 insertions(+), 3 deletions(-) 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: -- 2.39.5