From a3b28bc1d6f5f98308d9bfa027f8ae5031e7085d Mon Sep 17 00:00:00 2001 From: Davte Date: Tue, 5 Mar 2024 18:50:03 +0100 Subject: [PATCH] Allow to overwrite file when using bot.download_file method --- davtelepot/__init__.py | 2 +- davtelepot/bot.py | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/davtelepot/__init__.py b/davtelepot/__init__.py index e8ca8b4..4ebc1b7 100644 --- a/davtelepot/__init__.py +++ b/davtelepot/__init__.py @@ -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" diff --git a/davtelepot/bot.py b/davtelepot/bot.py index 1348d57..ed2cfc7 100644 --- a/davtelepot/bot.py +++ b/davtelepot/bot.py @@ -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)