From 7cfecfaf2158541c4025888a0aea011952be5b6f Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sat, 16 Jun 2018 17:01:20 +0200 Subject: [PATCH] Support pathlib.Path on download/upload --- telethon/client/downloads.py | 5 ++++- telethon/client/uploads.py | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/telethon/client/downloads.py b/telethon/client/downloads.py index 9fe65ec0..731a308c 100644 --- a/telethon/client/downloads.py +++ b/telethon/client/downloads.py @@ -2,10 +2,10 @@ import datetime import io import logging import os +import pathlib from .users import UserMethods from .. import utils, helpers, errors -from ..crypto import CdnDecrypter from ..tl import TLObject, types, functions __log__ = logging.getLogger(__name__) @@ -367,6 +367,9 @@ class DownloadMethods(UserMethods): If any modification is made to the path, this method will ensure that no existing file will be overwritten. """ + if isinstance(file, pathlib.Path): + file = str(file.absolute()) + if file is not None and not isinstance(file, str): # Probably a stream-like object, we cannot set a filename here return file diff --git a/telethon/client/uploads.py b/telethon/client/uploads.py index b6a162fe..c52fdae5 100644 --- a/telethon/client/uploads.py +++ b/telethon/client/uploads.py @@ -2,6 +2,7 @@ import hashlib import io import logging import os +import pathlib import warnings from io import BytesIO from mimetypes import guess_type @@ -361,6 +362,9 @@ class UploadMethods(MessageParseMethods, UserMethods): if not file: return None, None + if isinstance(file, pathlib.Path): + file = str(file.absolute()) + if not isinstance(file, (str, bytes, io.IOBase)): # The user may pass a Message containing media (or the media, # or anything similar) that should be treated as a file. Try