From b2d542ad40b20d0b67bad99a80173b64cf0ab6e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Fri, 17 Jan 2020 23:27:13 +0100 Subject: [PATCH] improve PathFormat._enum_file() open only one try-except block for the whole loop, instead of one for each iteration in os.path.exists() --- gallery_dl/util.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/gallery_dl/util.py b/gallery_dl/util.py index f426829d..994ce0f1 100644 --- a/gallery_dl/util.py +++ b/gallery_dl/util.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2017-2019 Mike Fährmann +# Copyright 2017-2020 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as @@ -602,12 +602,15 @@ class PathFormat(): def _enum_file(self): num = 1 - while True: - self.prefix = str(num) + "." - self.set_extension(self.extension, False) - if not os.path.exists(self.realpath): - return False - num += 1 + try: + while True: + self.prefix = str(num) + "." + self.set_extension(self.extension, False) + os.stat(self.realpath) # raises OSError if file doesn't exist + num += 1 + except OSError: + pass + return False def set_directory(self, kwdict): """Build directory path and create it if necessary""" @@ -623,7 +626,7 @@ class PathFormat(): except Exception as exc: raise exception.DirectoryFormatError(exc) - # Join path segements + # Join path segments sep = os.sep directory = self.clean_path(self.basedirectory + sep.join(segments))