wechaty / python-wechaty

Python Wechaty is a Conversational RPA SDK for Chatbot Makers written in Python
https://wechaty.readthedocs.io/zh_CN/latest/
Apache License 2.0
1.62k stars 234 forks source link

⁉ ⁉如何获取消息中的文件大小 #311

Open 17186765070 opened 2 years ago

17186765070 commented 2 years ago

requirements

Describe your problem

如何获取消息中的文件大小,通过如下方式可以获取文件名称,如何获取文件大小呢?谢谢帮助

Reproduce your problem

if msg.type() == MessageType.MESSAGE_TYPE_ATTACHMENT:
    file_src = msg.payload.filename
wj-Mcat commented 2 years ago

There are different types of files, eg: Url, Stream, Base64 and Local file. There is a simple way for you to get the size of file:

# step 1 
file = file_box.to_file()

# step 2
file_size = os.path.getsize('d:/file.jpg')
print("File Size is :", file_size, "bytes")

Hope it can help you fix your problem.

17186765070 commented 2 years ago

Thank you for your reply @wj-Mcat

If I use to_ File() ,One of the problems I encounter now is that if the file is too large, the program will fail.

Therefore, I want to get the file size first. If it exceeds 2m, then I decide not to file_box.to_file() the file

wj-Mcat commented 2 years ago

@17186765070 yes, I’m sorry to say that there are some issues when solving big files, but we will fix it in next few days. You can keep eyes on this issue to get latest progress.