summaryrefslogtreecommitdiffstats
path: root/gallery_dl/util.py
diff options
context:
space:
mode:
Diffstat (limited to 'gallery_dl/util.py')
-rw-r--r--gallery_dl/util.py33
1 files changed, 25 insertions, 8 deletions
diff --git a/gallery_dl/util.py b/gallery_dl/util.py
index f426829..13bf80e 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
@@ -121,6 +121,20 @@ def expand_path(path):
return os.path.expandvars(os.path.expanduser(path))
+def remove_file(path):
+ try:
+ os.unlink(path)
+ except OSError:
+ pass
+
+
+def remove_directory(path):
+ try:
+ os.rmdir(path)
+ except OSError:
+ pass
+
+
def code_to_language(code, default=None):
"""Map an ISO 639-1 language code to its actual name"""
return CODES.get((code or "").lower(), default)
@@ -602,12 +616,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 +640,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))