aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl
diff options
context:
space:
mode:
authorSergey M․ <dstftw@gmail.com>2015-03-08 20:55:22 +0600
committerSergey M․ <dstftw@gmail.com>2015-03-08 20:55:22 +0600
commita2aaf4dbc6e5f5d345329b5a845111851453b6a6 (patch)
treefc9407b420014b57e54df5393fda3f4e4cfd6791 /youtube_dl
parentbdf6eee0aeed8df586569982eaaac04eecc0062d (diff)
[utils] Add sanitize_path
Diffstat (limited to 'youtube_dl')
-rw-r--r--youtube_dl/utils.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py
index 7426e2a1f..0f49d602e 100644
--- a/youtube_dl/utils.py
+++ b/youtube_dl/utils.py
@@ -311,6 +311,24 @@ def sanitize_filename(s, restricted=False, is_id=False):
return result
+def sanitize_path(s):
+ """Sanitizes and normalizes path on Windows"""
+ if sys.platform != 'win32':
+ return s
+ drive, _ = os.path.splitdrive(s)
+ unc, _ = os.path.splitunc(s)
+ unc_or_drive = unc or drive
+ norm_path = os.path.normpath(remove_start(s, unc_or_drive)).split(os.path.sep)
+ if unc_or_drive:
+ norm_path.pop(0)
+ sanitized_path = [
+ re.sub('[/<>:"\\|\\\\?\\*]', '#', path_part)
+ for path_part in norm_path]
+ if unc_or_drive:
+ sanitized_path.insert(0, unc_or_drive + os.path.sep)
+ return os.path.join(*sanitized_path)
+
+
def orderedSet(iterable):
""" Remove all duplicates from the input iterable """
res = []