But when I try the following code, get_body() is not found. How to get >get_body() to work?
Peng Yu <pengyu.ut@gmail.com> writes:
But when I try the following code, get_body() is not found. How to get >>get_body() to work?
Did you know that this post of mine here was posted to
Usenet with a Python script I wrote?
That Python script has a function to show the body of
a post before posting. The post is contained in a file,
so it reads the post from that file.
I copy it here, maybe it can help some people to see
how I do this.
# Python 3.5
import email
...
def showbody( file ): # lightly edited for posting on 2023-03-19
output = ''
msg = email.message_from_binary_file\
( file, policy=email.policy.default )
Jon Ribbens <jon+usenet@unequivocal.eu> writes:
(Also, I too find it annoying to have to avoid, but calling a local >>variable 'file' is somewhat suspect since it shadows the builtin.)
Thanks for your remarks, but I'm not aware
of such a predefined name "file"!
(Also, I too find it annoying to have to avoid, but calling a local
variable 'file' is somewhat suspect since it shadows the builtin.)
Hi,
https://docs.python.org/3/library/email.parser.html
It says "For MIME messages, the root object will return True from its is_multipart() method, and the subparts can be accessed via the
payload manipulation methods, such as get_body(), iter_parts(), and
walk()."
But when I try the following code, get_body() is not found. How to get get_body() to work?
$ python3 -c 'import email, sys; msg = email.message_from_string(sys.stdin.read()); print(msg.get_body())'
<<< some_text
Traceback (most recent call last):
File "<string>", line 1, in <module>
AttributeError: 'Message' object has no attribute 'get_body'
Ah, apparently it got removed in Python 3, which is a bit odd as the
last I heard it was added in Python 2.2 in order to achieve consistency
with other types.
On 20/03/23 7:07 am, Jon Ribbens wrote:
Ah, apparently it got removed in Python 3, which is a bit odd as the
last I heard it was added in Python 2.2 in order to achieve consistency
with other types.
As far as I remember, the file type came into existence
with type/class unification, and "open" became an alias
for the file type, so you could use open() and file()
interchangeably.
With the Unicode revolution in Python 3, file handling got
a lot more complicated. Rather than a single file type,
there are now a bunch of classes that handle low-level I/O, encoding/decoding, etc, and open() is a function again
that builds the appropriate combination of underlying
objects.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 39:54:29 |
Calls: | 10,392 |
Files: | 14,064 |
Messages: | 6,417,198 |