Cleaner way to use os.makedirs()

It's not necessary to do an os.path.exists(dst_dir) check before calling
os.makedirs(), the exist_ok=True parameter is meant for this common
pattern.
This commit is contained in:
2017-01-17 17:01:37 +01:00
parent 9e8f95114c
commit 94253975ca

View File

@@ -513,10 +513,8 @@ def pack(
if (not os.path.exists(src)) or os.path.isdir(src): if (not os.path.exists(src)) or os.path.isdir(src):
yield report(" %s: %r\n" % (colorize("source missing", color='red'), src)) yield report(" %s: %r\n" % (colorize("source missing", color='red'), src))
else: else:
dst_dir = os.path.dirname(dst)
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
yield report(" %s: %r -> %r\n" % (colorize("copying", color='blue'), src, dst)) yield report(" %s: %r -> %r\n" % (colorize("copying", color='blue'), src, dst))
os.makedirs(os.path.dirname(dst), exist_ok=True)
shutil.copy(src, dst) shutil.copy(src, dst)
shutil.rmtree(base_dir_dst_temp) shutil.rmtree(base_dir_dst_temp)