From ff52a69b91f68997bb8cb7b95acf513089afdc48 Mon Sep 17 00:00:00 2001 From: andreabravetti Date: Mon, 28 May 2012 15:07:38 +0300 Subject: [PATCH] do not overwrite existing files --- youtube_dl/utils.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index ae30da53e..4c65bd070 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -166,6 +166,22 @@ def sanitize_open(filename, open_mode): It returns the tuple (stream, definitive_file_name). """ + + # do not overwrite existing files + if filename != u'-': + if filename.endswith(u'.part'): + _addpart = u'.part' + _name, _ext = os.path.splitext(filename[:-len(u'.part')]) + filename = filename[:-len(u'.part')] + else: + _addpart = u'' + _name, _ext = os.path.splitext(filename) + _count = 0 + while os.path.isfile(filename): + _count += 1 + filename = "%s-%s%s" % (_name, _count, _ext) + filename = "%s%s" % (filename,_addpart) + try: if filename == u'-': if sys.platform == 'win32':