Allow to overwrite file when using bot.download_file method

This commit is contained in:
Davte 2024-03-05 18:50:03 +01:00
parent 3969794075
commit a3b28bc1d6
Signed by: Davte
GPG Key ID: 70336F92E6814706
2 changed files with 6 additions and 4 deletions

View File

@ -11,7 +11,7 @@ __author__ = "Davide Testa"
__email__ = "davide@davte.it"
__credits__ = ["Marco Origlia", "Nick Lee @Nickoala"]
__license__ = "GNU General Public License v3.0"
__version__ = "2.9.11"
__version__ = "2.9.12"
__maintainer__ = "Davide Testa"
__contact__ = "t.me/davte"

View File

@ -2127,7 +2127,8 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
return sent_update
async def download_file(self, file_id,
file_name=None, path=None):
file_name=None, path=None,
prevent_overwriting: bool = False):
"""Given a telegram `file_id`, download the related file.
Telegram may not preserve the original file name and MIME type: the
@ -2151,8 +2152,9 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
if file_name is None:
file_name = get_secure_key(length=10)
file_complete_path = os.path.join(path, file_name)
while os.path.exists(file_complete_path):
file_complete_path = file_complete_path + '1'
if prevent_overwriting:
while os.path.exists(file_complete_path):
file_complete_path = file_complete_path + '1'
try:
with open(file_complete_path, 'wb') as local_file:
local_file.write(file_bytes)