From 9d2607a048f8e41b0bf55b25e8fbf8b96a03a091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Thu, 20 Dec 2018 16:46:28 -0500 Subject: [PATCH 1/3] Use makedirs when needed in save_content_to_file --- gwhat/common/utils.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gwhat/common/utils.py b/gwhat/common/utils.py index 01e975b85..2995d1a10 100644 --- a/gwhat/common/utils.py +++ b/gwhat/common/utils.py @@ -59,7 +59,11 @@ def save_content_to_file(fname, fcontent): Smart function that checks the extension and save the content in the appropriate file format. """ - root, ext = os.path.splitext(fname) + dirname = osp.dirname(fname) + if not osp.exists(dirname): + os.makedirs(dirname) + + root, ext = osp.splitext(fname) if ext in ['.xlsx', '.xls']: save_content_to_excel(fname, fcontent) elif ext == '.tsv': From d2761373f1c33f9baf039cc3bdeca8c2fc41696e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Thu, 20 Dec 2018 16:54:41 -0500 Subject: [PATCH 2/3] Add create_dirname to utils module Create the dirname of a file if it doesn't exists. --- gwhat/common/utils.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/gwhat/common/utils.py b/gwhat/common/utils.py index 2995d1a10..48500d2f1 100644 --- a/gwhat/common/utils.py +++ b/gwhat/common/utils.py @@ -100,6 +100,13 @@ def save_content_to_excel(fname, fcontent): ws.write_row(i, 0, row) +def create_dirname(fname): + """Create the dirname of a file if it doesn't exists.""" + dirname = osp.dirname(fname) + if dirname and not osp.exists(dirname): + os.makedirs(dirname) + + def delete_file(filename): """Try to delete a file on the disk and return the error if any.""" try: From 90eb8af95c8f56d824ede4f596f4d6a293bef212 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Thu, 20 Dec 2018 16:55:05 -0500 Subject: [PATCH 3/3] Use create_dirname func where required --- gwhat/common/utils.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/gwhat/common/utils.py b/gwhat/common/utils.py index 48500d2f1..f2f32c39c 100644 --- a/gwhat/common/utils.py +++ b/gwhat/common/utils.py @@ -59,10 +59,6 @@ def save_content_to_file(fname, fcontent): Smart function that checks the extension and save the content in the appropriate file format. """ - dirname = osp.dirname(fname) - if not osp.exists(dirname): - os.makedirs(dirname) - root, ext = osp.splitext(fname) if ext in ['.xlsx', '.xls']: save_content_to_excel(fname, fcontent) @@ -78,6 +74,7 @@ def save_content_to_csv(fname, fcontent, mode='w', delimiter=',', Save content in a csv file with the specifications provided in arguments. """ + create_dirname(fname) with open(fname, mode, encoding='utf8') as csvfile: writer = csv.writer(csvfile, delimiter=delimiter, lineterminator='\n') writer.writerows(fcontent) @@ -85,6 +82,7 @@ def save_content_to_csv(fname, fcontent, mode='w', delimiter=',', def save_content_to_excel(fname, fcontent): """Save content in a xls or xlsx file.""" + create_dirname(fname) root, ext = os.path.splitext(fname) if ext == '.xls': wb = xlwt.Workbook()