Don't LBYL when creating missing data directories
authorJeremy Stanley <fungi@yuggoth.org>
Sat, 27 Oct 2018 21:01:52 +0000 (21:01 +0000)
committerJeremy Stanley <fungi@yuggoth.org>
Sat, 27 Oct 2018 21:01:52 +0000 (21:01 +0000)
It's better to ask forgiveness than permission. Just try to create
parent directories for any data file, and don't error if they
already exist.

mudpy/data.py

index 25b423b..b73959a 100644 (file)
@@ -111,10 +111,9 @@ class Data:
            ):
 
             # make parent directories if necessary
-            if not os.path.exists(os.path.dirname(self.source)):
-                old_umask = os.umask(normal_umask)
-                os.makedirs(os.path.dirname(self.source))
-                os.umask(old_umask)
+            old_umask = os.umask(normal_umask)
+            os.makedirs(os.path.dirname(self.source), exist_ok=True)
+            os.umask(old_umask)
 
             # backup the file
             if "mudpy.limit" in self.universe.contents: