1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-23 03:02:50 +01:00

delay directory creation (#2461, #2474)

Only call os.makedirs() when an open() call
fails with a FileNotFoundError

Might cause errors with some post processors etc,
but for now I was unable to find any.
This commit is contained in:
Mike Fährmann 2022-05-03 23:50:12 +02:00
parent 4c8e28db82
commit 99cb28724f
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

View File

@ -74,7 +74,7 @@ class PathFormat():
self.directory = self.realdirectory = \
self.filename = self.extension = self.prefix = \
self.path = self.realpath = self.temppath = ""
self.delete = self._create_directory = False
self.delete = False
extension_map = config("extension-map")
if extension_map is None:
@ -138,7 +138,11 @@ class PathFormat():
def open(self, mode="wb"):
"""Open file and return a corresponding file object"""
return open(self.temppath, mode)
try:
return open(self.temppath, mode)
except FileNotFoundError:
os.makedirs(self.realdirectory, exist_ok=True)
return open(self.temppath, mode)
def exists(self):
"""Return True if the file exists on disk"""
@ -187,7 +191,6 @@ class PathFormat():
directory += sep
self.realdirectory = directory
self._create_directory = True
def set_filename(self, kwdict):
"""Set general filename data"""
@ -279,9 +282,6 @@ class PathFormat():
def build_path(self):
"""Combine directory and filename to full paths"""
if self._create_directory:
os.makedirs(self.realdirectory, exist_ok=True)
self._create_directory = False
self.filename = filename = self.build_filename(self.kwdict)
self.path = self.directory + filename
self.realpath = self.realdirectory + filename