Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The reason computer chips are so small is computers don't eat much.


devel / comp.lang.python / Re: How to iterate through maildir messages in python 3?

SubjectAuthor
* How to iterate through maildir messages in python 3?Chris Green
`* Re: How to iterate through maildir messages in python 3?Greg Ewing
 `* Re: How to iterate through maildir messages in python 3?Chris Green
  +* Re: How to iterate through maildir messages in python 3?Gilmeh Serda
  |`- Re: How to iterate through maildir messages in python 3?Chris Green
  +* Re: How to iterate through maildir messages in python 3?Chris Angelico
  |`- Re: How to iterate through maildir messages in python 3?Chris Green
  `* Re: How to iterate through maildir messages in python 3?Dennis Lee Bieber
   `- Re: How to iterate through maildir messages in python 3?Chris Green

1
How to iterate through maildir messages in python 3?

<1m1fqh-32gb.ln1@esprimo.zbmc.eu>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=13905&group=comp.lang.python#13905

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: cl...@isbd.net (Chris Green)
Newsgroups: comp.lang.python
Subject: How to iterate through maildir messages in python 3?
Date: Thu, 24 Jun 2021 20:06:09 +0100
Lines: 19
Message-ID: <1m1fqh-32gb.ln1@esprimo.zbmc.eu>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net +iFw2rNXn4UFvNCIe0+dcA9WESDbpKRJ+zJiSpp90VPBZqe20=
X-Orig-Path: not-for-mail
Cancel-Lock: sha1:vDSw9aKl9QGIjUl/pbwi9Y3NUD0=
User-Agent: tin/2.4.5-20201224 ("Glen Albyn") (Linux/5.11.0-18-generic (x86_64))
 by: Chris Green - Thu, 24 Jun 2021 19:06 UTC

In python 2 one can do:-

for msg in maildir:
print msg # or whatever you want to do with the message

However in python 3 this produces "TypeError: string argument
expected, got 'bytes'".

How should one iterate over a maildir in python3?

(I've been here before but this problem is subtly different from the
one I had before and I can't find the answer)

--
Chris Green
·

Re: How to iterate through maildir messages in python 3?

<ijklskF49sgU1@mid.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=13916&group=comp.lang.python#13916

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: greg.ew...@canterbury.ac.nz (Greg Ewing)
Newsgroups: comp.lang.python
Subject: Re: How to iterate through maildir messages in python 3?
Date: Fri, 25 Jun 2021 12:21:40 +1200
Lines: 30
Message-ID: <ijklskF49sgU1@mid.individual.net>
References: <1m1fqh-32gb.ln1@esprimo.zbmc.eu>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net TvF4477/ZdbSv9AXXpqrSgK2mIfq7TNclNQz+BKj3RXHyQ1MPO
Cancel-Lock: sha1:PEiPHuD7nBl/l8GDm/77Oqqdu4Q=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:78.0)
Gecko/20100101 Thunderbird/78.4.0
In-Reply-To: <1m1fqh-32gb.ln1@esprimo.zbmc.eu>
Content-Language: en-US
 by: Greg Ewing - Fri, 25 Jun 2021 00:21 UTC

On 25/06/21 7:06 am, Chris Green wrote:
> In python 2 one can do:-
>
> for msg in maildir:
> print msg # or whatever you want to do with the message
>
>
> However in python 3 this produces "TypeError: string argument
> expected, got 'bytes'".
>
> How should one iterate over a maildir in python3?

You're already iterating over it just fine. Your problem is
actually how to *print* a mail message.

The answer to this will depend on what your purpose is. If
you just want a rough-and-ready idea of what the message
contains, you could do this:

print(repr(msg))

However, that won't work very well if the message doesn't
consist of mostly text in an ASCII-compatible encoding.

If you want something better, Python comes with some standard
library code for dealing with mail messages. Check out the
'email' module.

--
Greg

Re: How to iterate through maildir messages in python 3?

<56ggqh-v2de.ln1@esprimo.zbmc.eu>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=13920&group=comp.lang.python#13920

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: cl...@isbd.net (Chris Green)
Newsgroups: comp.lang.python
Subject: Re: How to iterate through maildir messages in python 3?
Date: Fri, 25 Jun 2021 09:19:49 +0100
Lines: 94
Message-ID: <56ggqh-v2de.ln1@esprimo.zbmc.eu>
References: <1m1fqh-32gb.ln1@esprimo.zbmc.eu> <ijklskF49sgU1@mid.individual.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net hAUC6nG1s6u/rXATwTSqSwHEN5CMJB1IFi2ttFcSNlnvCPczU=
X-Orig-Path: not-for-mail
Cancel-Lock: sha1:tjho37tMFsuT1ah7tHcXgDYVa+w=
User-Agent: tin/2.4.5-20201224 ("Glen Albyn") (Linux/5.11.0-18-generic (x86_64))
 by: Chris Green - Fri, 25 Jun 2021 08:19 UTC

Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
> On 25/06/21 7:06 am, Chris Green wrote:
> > In python 2 one can do:-
> >
> > for msg in maildir:
> > print msg # or whatever you want to do with the message
> >
> >
> > However in python 3 this produces "TypeError: string argument
> > expected, got 'bytes'".
> >
> > How should one iterate over a maildir in python3?
>
> You're already iterating over it just fine. Your problem is
> actually how to *print* a mail message.
>
> The answer to this will depend on what your purpose is. If
> you just want a rough-and-ready idea of what the message
> contains, you could do this:
>
> print(repr(msg))
>
> However, that won't work very well if the message doesn't
> consist of mostly text in an ASCII-compatible encoding.
>
> If you want something better, Python comes with some standard
> library code for dealing with mail messages. Check out the
> 'email' module.
>
The error comes from the line "for msg in maildir:", not the print.

Here's the full program where I'm encountering the error (yes, I
should have posted this first time around) :-

#!/usr/bin/python3

import mailbox
import sys
import email

# open the existing maildir and the target mbox file
maildir = mailbox.Maildir(sys.argv [-2], email.message_from_file)
mbox = mailbox.mbox(sys.argv[-1])

# lock the mbox
mbox.lock()

# iterate over messages in the maildir and add to the mbox
for msg in maildir:
mbox.add(msg)

# close and unlock
mbox.close()
maildir.close()

(... and it's not my coding style, just copied)

Here's the error trace:-

chris$ ./md.py houseSitting fred
Traceback (most recent call last):
File "/home/chris/tmp/./md.py", line 18, in <module>
for msg in maildir:
File "/usr/lib/python3.9/mailbox.py", line 110, in itervalues
value = self[key]
File "/usr/lib/python3.9/mailbox.py", line 77, in __getitem__
return self._factory(file)
File "/usr/lib/python3.9/email/__init__.py", line 54, in message_from_file
return Parser(*args, **kws).parse(fp)
File "/usr/lib/python3.9/email/parser.py", line 56, in parse
feedparser.feed(data)
File "/usr/lib/python3.9/email/feedparser.py", line 175, in feed
self._input.push(data)
File "/usr/lib/python3.9/email/feedparser.py", line 103, in push
self._partial.write(data)
TypeError: string argument expected, got 'bytes'
chris$

Line 18 is the "for msg in maildir:".

Presumably the program worked as posted under python 2, all I have
done is to change the shebang line to use python 3 (I no longer have
python 2 on my system, otherwise I'd have used it as this is only a
quick and dirty conversion script to be used once).

--
Chris Green
·

Re: How to iterate through maildir messages in python 3?

<F9kBI.431099$N5n7.255777@fx05.ams4>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=13921&group=comp.lang.python#13921

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!paganini.bofh.team!news.dns-netz.com!news.freedyn.net!newsreader4.netcologne.de!news.netcologne.de!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx05.ams4.POSTED!not-for-mail
From: gilmeh.s...@nothing.here.invalid (Gilmeh Serda)
Subject: Re: How to iterate through maildir messages in python 3?
Newsgroups: comp.lang.python
References: <1m1fqh-32gb.ln1@esprimo.zbmc.eu>
<ijklskF49sgU1@mid.individual.net> <56ggqh-v2de.ln1@esprimo.zbmc.eu>
X-No-Archive: Yes
User-Agent: Pan/0.136 (I'm far too busy being delicious; GIT 926a150
git://git.gnome.org/pan2)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Lines: 14
Message-ID: <F9kBI.431099$N5n7.255777@fx05.ams4>
X-Complaints-To: abuse@easynews.com
Organization: Easynews - www.easynews.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Fri, 25 Jun 2021 12:41:09 GMT
X-Received-Bytes: 1249
 by: Gilmeh Serda - Fri, 25 Jun 2021 12:41 UTC

On Fri, 25 Jun 2021 09:19:49 +0100, Chris Green wrote:

> TypeError: string argument expected, got 'bytes'

couple things comes to mind:

1. find py2 as archive, put it somewhere and run it from that

2. convert the bytes to str (find and replace)

3. run the py2>py3 conversion script

--
Gilmeh

Re: How to iterate through maildir messages in python 3?

<6h0hqh-64jf.ln1@esprimo.zbmc.eu>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=13922&group=comp.lang.python#13922

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: cl...@isbd.net (Chris Green)
Newsgroups: comp.lang.python
Subject: Re: How to iterate through maildir messages in python 3?
Date: Fri, 25 Jun 2021 13:58:46 +0100
Lines: 23
Message-ID: <6h0hqh-64jf.ln1@esprimo.zbmc.eu>
References: <1m1fqh-32gb.ln1@esprimo.zbmc.eu> <ijklskF49sgU1@mid.individual.net> <56ggqh-v2de.ln1@esprimo.zbmc.eu> <F9kBI.431099$N5n7.255777@fx05.ams4>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net CefcdMUnE+gmYEy5FUpfJQC9+bHOKBDOPnP9we5DRcZrLS4nM=
X-Orig-Path: not-for-mail
Cancel-Lock: sha1:SCDvX4hkpk9fMww7KjE5CXcgf64=
User-Agent: tin/2.4.5-20201224 ("Glen Albyn") (Linux/5.11.0-18-generic (x86_64))
 by: Chris Green - Fri, 25 Jun 2021 12:58 UTC

Gilmeh Serda <gilmeh.serdah@nothing.here.invalid> wrote:
> On Fri, 25 Jun 2021 09:19:49 +0100, Chris Green wrote:
>
> > TypeError: string argument expected, got 'bytes'
>
> couple things comes to mind:
>
> 1. find py2 as archive, put it somewhere and run it from that
>
Hmm! :-)

> 2. convert the bytes to str (find and replace)
>
How? It's failing at "for msg in maildir:", you can't change it to
"for str(msg) in maildir" (well you can but it gives a syntax error).

> 3. run the py2>py3 conversion script
>
That does nothing, says "No files need to be modified"

--
Chris Green
·

Re: How to iterate through maildir messages in python 3?

<mailman.166.1624636839.4164.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=13924&group=comp.lang.python#13924

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: How to iterate through maildir messages in python 3?
Date: Sat, 26 Jun 2021 02:00:26 +1000
Lines: 60
Message-ID: <mailman.166.1624636839.4164.python-list@python.org>
References: <1m1fqh-32gb.ln1@esprimo.zbmc.eu>
<ijklskF49sgU1@mid.individual.net>
<56ggqh-v2de.ln1@esprimo.zbmc.eu>
<CAPTjJmpTNf0RfxJ0T9u5RVq-e0hOei9Be-4KvtLZh_f2U_tLZg@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de tCxeF62HWUaXjukMfZ+ODQ9HyOTgSicUjufUPFS8Mztg==
Return-Path: <rosuav@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=VH5YBvN3;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.002
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'this:': 0.03; 'argument':
0.04; 'is.': 0.05; 'string': 0.05; 'subject:python': 0.06; '26,':
0.07; 'better,': 0.09; 'depend': 0.09; 'messages.': 0.09;
'module.': 0.09; 'looks': 0.11; 'import': 0.14; 'problem': 0.15;
'chrisa': 0.16; 'encoding.': 0.16; 'from:addr:rosuav': 0.16;
'from:name:chris angelico': 0.16; 'greg': 0.16; 'iterate': 0.16;
'object,': 0.16; 'wrote:': 0.16; 'says': 0.16; 'python': 0.16;
'subject:How': 0.22; 'sat,': 0.23; 'to:addr:python-list': 0.23;
'code': 0.24; 'idea': 0.25; 'chris': 0.26; 'purpose': 0.26;
'binary': 0.27; 'jun': 0.27; 'library': 0.27; 'function': 0.28;
'mostly': 0.28; 'error': 0.28; 'text': 0.29; 'it,': 0.31; 'but':
0.31; 'am,': 0.31; "doesn't": 0.32; "i'm": 0.32; 'fine.': 0.32;
'to:name:python': 0.32; 'message-id:@mail.gmail.com': 0.33;
'program': 0.33; 'received:209.85.166': 0.33; 'header:In-Reply-
To:1': 0.33; 'received:google.com': 0.34; 'from:addr:gmail.com':
0.35; 'received:209.85': 0.38; 'file': 0.38; 'something': 0.38;
'received:209': 0.38; 'messages': 0.40; 'comes': 0.40; 'could':
0.40; 'check': 0.62; 'email': 0.62; 'skip:m 20': 0.64; 'skip:#
10': 0.64; 'your': 0.64; 'well': 0.66; 'similar': 0.66; 'full':
0.68; 'subject:through': 0.69; 'skip:e 20': 0.70; '2021': 0.84;
'want.': 0.84; 'factory': 0.91; 'mailbox': 0.91; 'produces': 0.93;
'green': 0.96
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to;
bh=Q5YnrUBCPNaNhI/a0F5zIV3GKr4jjdkgRLWmOt1ykZk=;
b=VH5YBvN3a51+qZFcjTDwqYCzahWdqep88Eg7pS46vfWng5hM1Pg0jPwrq4J0LNpRsT
lM4f+5tBQYgDdQP0uQttz8MIw+KfIZ0+Miz4jaItCAJ8AnWzL1bJpyzLSClr3lj76Xws
KHcZtlzgOz1EspxStE08Lum06FVxbjZWINShWdrv+NLJGIG1FXIvAR/GqfnKf7ogJ/6X
39/V5gdULy07kCczT9CcmsnKXCdUToQonYMXCauk2epBiIB3AkF7FZXpn6xdyainDF+V
TO1f+Ll/h8IG/1N75qpMFVNEDPu27lPMcF4G7ExK/8KVIDZRDRpoSWg2k414Ja4VGAkb
thTw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20161025;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to;
bh=Q5YnrUBCPNaNhI/a0F5zIV3GKr4jjdkgRLWmOt1ykZk=;
b=nICExy9+Tt/uqlgXEzNcXqT+Kh/OVFZit28dL4sbt+mzbVF0yEUornBNuhjoUyfOH6
J8EjgNO0toPunfvrOzsX7X3eCSX+JOjlckMLV2HabATILVpGeEHb3o0QhdQxAfPKO4Pv
GTTxKA3wSNe0Na7+eBQAHq/95/7ieSL/netyEdopsjpC9KIztsrdevekph5pLBbOxGVT
0tRCHTBe8mcsAStXx3rXjQ7DXvTKDlhJ43wg2PMV/fk9qfJAE1r6IJ2Pmz4/EHpBBANH
LWFi86iXqOTukWmcaKuW8mZftywIS9rKZrFcKdVUE1ByReKGshVwi3+etxk+qUTjffAc
auoA==
X-Gm-Message-State: AOAM532clExKXseWkuFWEerGM1/UfIXAmKt2S0SNSRiHa/+2ARSX0vEJ
f0UZChdDZ4m5U0rWIKJPEILDgHHnxc5X6DyYWzVPKmMf
X-Google-Smtp-Source: ABdhPJxVj8Ord32cwkcHEcj4/dMh9XsXIAEkFJK0w9k1Mi94J73gzwiGIbOUcDNsmTjLdFGlk+pk89bu2YkGbkPGNDI=
X-Received: by 2002:a5d:83ce:: with SMTP id u14mr9215780ior.45.1624636837383;
Fri, 25 Jun 2021 09:00:37 -0700 (PDT)
In-Reply-To: <56ggqh-v2de.ln1@esprimo.zbmc.eu>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.34
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAPTjJmpTNf0RfxJ0T9u5RVq-e0hOei9Be-4KvtLZh_f2U_tLZg@mail.gmail.com>
X-Mailman-Original-References: <1m1fqh-32gb.ln1@esprimo.zbmc.eu>
<ijklskF49sgU1@mid.individual.net>
<56ggqh-v2de.ln1@esprimo.zbmc.eu>
 by: Chris Angelico - Fri, 25 Jun 2021 16:00 UTC

On Sat, Jun 26, 2021 at 12:28 AM Chris Green <cl@isbd.net> wrote:
>
> Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
> > On 25/06/21 7:06 am, Chris Green wrote:
> > > In python 2 one can do:-
> > >
> > > for msg in maildir:
> > > print msg # or whatever you want to do with the message
> > >
> > >
> > > However in python 3 this produces "TypeError: string argument
> > > expected, got 'bytes'".
> > >
> > > How should one iterate over a maildir in python3?
> >
> > You're already iterating over it just fine. Your problem is
> > actually how to *print* a mail message.
> >
> > The answer to this will depend on what your purpose is. If
> > you just want a rough-and-ready idea of what the message
> > contains, you could do this:
> >
> > print(repr(msg))
> >
> > However, that won't work very well if the message doesn't
> > consist of mostly text in an ASCII-compatible encoding.
> >
> > If you want something better, Python comes with some standard
> > library code for dealing with mail messages. Check out the
> > 'email' module.
> >
> The error comes from the line "for msg in maildir:", not the print.
>
> Here's the full program where I'm encountering the error (yes, I
> should have posted this first time around) :-
>
> #!/usr/bin/python3
>
> import mailbox
> import sys
> import email
>
>
> # open the existing maildir and the target mbox file
> maildir = mailbox.Maildir(sys.argv [-2], email.message_from_file)
> mbox = mailbox.mbox(sys.argv[-1])
>
> # iterate over messages in the maildir and add to the mbox
> for msg in maildir:
> mbox.add(msg)
>

Maildir says that the factory has to take a binary file object. It
looks like email.message_from_file is expecting a text file object,
but there's a very similar function message_from_binary_file that
might be what you want.

Haven't tested it, but worth a try.

ChrisA

Re: How to iterate through maildir messages in python 3?

<luhhqh-3dkg.ln1@esprimo.zbmc.eu>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=13925&group=comp.lang.python#13925

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: cl...@isbd.net (Chris Green)
Newsgroups: comp.lang.python
Subject: Re: How to iterate through maildir messages in python 3?
Date: Fri, 25 Jun 2021 18:56:05 +0100
Lines: 97
Message-ID: <luhhqh-3dkg.ln1@esprimo.zbmc.eu>
References: <1m1fqh-32gb.ln1@esprimo.zbmc.eu> <ijklskF49sgU1@mid.individual.net> <56ggqh-v2de.ln1@esprimo.zbmc.eu> <CAPTjJmpTNf0RfxJ0T9u5RVq-e0hOei9Be-4KvtLZh_f2U_tLZg@mail.gmail.com> <mailman.166.1624636839.4164.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net X9Dp+pjxK+Sp5X3OaCEKuQeIJLYfJ8sB6rWdxlFwp+if1KNN0=
X-Orig-Path: not-for-mail
Cancel-Lock: sha1:FMYb64QdH1XxWz54JRBFG5Eo7pg=
User-Agent: tin/2.4.5-20201224 ("Glen Albyn") (Linux/5.11.0-18-generic (x86_64))
 by: Chris Green - Fri, 25 Jun 2021 17:56 UTC

Chris Angelico <rosuav@gmail.com> wrote:
> On Sat, Jun 26, 2021 at 12:28 AM Chris Green <cl@isbd.net> wrote:
> >
> > Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
> > > On 25/06/21 7:06 am, Chris Green wrote:
> > > > In python 2 one can do:-
> > > >
> > > > for msg in maildir:
> > > > print msg # or whatever you want to do with the message
> > > >
> > > >
> > > > However in python 3 this produces "TypeError: string argument
> > > > expected, got 'bytes'".
> > > >
> > > > How should one iterate over a maildir in python3?
> > >
> > > You're already iterating over it just fine. Your problem is
> > > actually how to *print* a mail message.
> > >
> > > The answer to this will depend on what your purpose is. If
> > > you just want a rough-and-ready idea of what the message
> > > contains, you could do this:
> > >
> > > print(repr(msg))
> > >
> > > However, that won't work very well if the message doesn't
> > > consist of mostly text in an ASCII-compatible encoding.
> > >
> > > If you want something better, Python comes with some standard
> > > library code for dealing with mail messages. Check out the
> > > 'email' module.
> > >
> > The error comes from the line "for msg in maildir:", not the print.
> >
> > Here's the full program where I'm encountering the error (yes, I
> > should have posted this first time around) :-
> >
> > #!/usr/bin/python3
> >
> > import mailbox
> > import sys
> > import email
> >
> >
> > # open the existing maildir and the target mbox file
> > maildir = mailbox.Maildir(sys.argv [-2], email.message_from_file)
> > mbox = mailbox.mbox(sys.argv[-1])
> >
> > # iterate over messages in the maildir and add to the mbox
> > for msg in maildir:
> > mbox.add(msg)
> >
>
> Maildir says that the factory has to take a binary file object. It
> looks like email.message_from_file is expecting a text file object,
> but there's a very similar function message_from_binary_file that
> might be what you want.
>
> Haven't tested it, but worth a try.
>
Even simpler, just remove email.message_from_file! :-)

The following works fine:-

#!/usr/bin/python3

import mailbox
import sys
import email

# open the existing maildir and the target mbox file
maildir = mailbox.Maildir(sys.argv [-2])
mbox = mailbox.mbox(sys.argv[-1])

# lock the mbox
mbox.lock()

# iterate over messages in the maildir and add to the mbox
for msg in maildir:
mbox.add(msg)

# close and unlock
mbox.close()
maildir.close()

Thank you for pointing me in the right direction.

--
Chris Green
·

Re: How to iterate through maildir messages in python 3?

<mailman.168.1624758047.4164.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=13928&group=comp.lang.python#13928

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: wlfr...@ix.netcom.com (Dennis Lee Bieber)
Newsgroups: comp.lang.python
Subject: Re: How to iterate through maildir messages in python 3?
Date: Fri, 25 Jun 2021 12:46:04 -0400
Organization: IISS Elusive Unicorn
Lines: 67
Message-ID: <mailman.168.1624758047.4164.python-list@python.org>
References: <1m1fqh-32gb.ln1@esprimo.zbmc.eu>
<ijklskF49sgU1@mid.individual.net> <56ggqh-v2de.ln1@esprimo.zbmc.eu>
<7g0cdgpen22qgs1a805j3qshdpkmp2bqo7@4ax.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de uy/f2+AF6gDFK9px3yWq3QzGM3dYVSLnoifzmUcKVFaQ==
Return-Path: <python-python-list@m.gmane-mx.org>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.015
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; '(which': 0.04;
'parameter': 0.05; 'subject:python': 0.06; 'else.': 0.07; 'utf-8':
0.07; '"""': 0.09; 'instances': 0.09; 'received:ciao.gmane.io':
0.09; 'received:gmane.io': 0.09; 'received:list': 0.09; 'import':
0.14; 'conversion': 0.16; 'file-like': 0.16; 'instance': 0.16;
'message-id:@4ax.com': 0.16; 'received:116.202': 0.16;
'received:116.202.254': 0.16; 'received:116.202.254.214': 0.16;
'unicode': 0.16; 'python': 0.16; 'subject:How': 0.22; 'fri,':
0.23; 'object': 0.23; 'returns': 0.23; "i'd": 0.23; 'to:addr
:python-list': 0.23; 'chris': 0.26; 'binary': 0.27; 'jun': 0.27;
'default': 0.28; 'error': 0.28; 'header:User-Agent:1': 0.31;
'header:Organization:1': 0.31; 'there': 0.31; 'script': 0.32;
"i'm": 0.32; 'specified': 0.32; 'program': 0.33; 'unless': 0.33;
'system,': 0.37; 'change': 0.37; 'file': 0.38; 'something': 0.38;
'use': 0.38; 'url-ip:151.101.36.223/32': 0.38; 'url-
ip:151.101.36/24': 0.38; 'does': 0.38; 'list': 0.39; 'reason':
0.40; 'called': 0.40; 'messages': 0.40; 'simple': 0.40;
'otherwise': 0.40; 'skip:e 10': 0.60; 'email': 0.62; 'worked':
0.63; 'skip:m 20': 0.64; 'skip:# 10': 0.64; 'full': 0.68; 'that,':
0.68; 'mailboxes': 0.69; 'subject:through': 0.69; 'skip:e 20':
0.70; 'received:116': 0.71; 'longer': 0.72; '2021': 0.84;
'represented': 0.84; 'strings': 0.84; 'url-ip:76/8': 0.84;
'factory': 0.91; 'mailbox': 0.91; 'green': 0.96
X-Injected-Via-Gmane: http://gmane.org/
User-Agent: ForteAgent/8.00.32.1272
X-No-Archive: YES
X-Mailman-Approved-At: Sat, 26 Jun 2021 21:40:45 -0400
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.34
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <7g0cdgpen22qgs1a805j3qshdpkmp2bqo7@4ax.com>
X-Mailman-Original-References: <1m1fqh-32gb.ln1@esprimo.zbmc.eu>
<ijklskF49sgU1@mid.individual.net> <56ggqh-v2de.ln1@esprimo.zbmc.eu>
 by: Dennis Lee Bieber - Fri, 25 Jun 2021 16:46 UTC

On Fri, 25 Jun 2021 09:19:49 +0100, Chris Green <cl@isbd.net> declaimed the
following:

>
>Here's the full program where I'm encountering the error (yes, I
>should have posted this first time around) :-
>
> #!/usr/bin/python3
>
> import mailbox
> import sys
> import email
>
>
> # open the existing maildir and the target mbox file
> maildir = mailbox.Maildir(sys.argv [-2], email.message_from_file)

Is there some reason you are overriding whatever the default "factory"
is for Maildir?

https://docs.python.org/3/library/mailbox.html#maildir
"""
itervalues()
__iter__()
values()

Return an iterator over representations of all messages if called as
itervalues() or __iter__() or return a list of such representations if
called as values(). The messages are represented as instances of the
appropriate format-specific Message subclass unless a custom message
factory was specified when the Mailbox instance was initialized.
"""
Emphasis: messages are represented as instances of the appropriate
format-specific Message subclass

"""
A subclass of Mailbox for mailboxes in Maildir format. Parameter factory is
a callable object that accepts a file-like message representation (which
behaves as if opened in binary mode) and returns a custom representation.
If factory is None, MaildirMessage is used as the default message
representation. If create is True, the mailbox is created if it does not
exist.
"""
Emphasis: If factory is None, MaildirMessage is used as the default message
representation.

>Presumably the program worked as posted under python 2, all I have
>done is to change the shebang line to use python 3 (I no longer have
>python 2 on my system, otherwise I'd have used it as this is only a
>quick and dirty conversion script to be used once).
>
Don't know if
https://docs.python.org/3/library/email.policy.html#email.policy.compat32
applies.

However, you do have to take into account that, in Python 2, strings
WERE bytes, and Unicode was special. In Python 3, strings are Unicode,
while lots of internet protocols define simple bytes -- you have to
encode/decode if the contents are UTF-8 or something else.
..

--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/

Re: How to iterate through maildir messages in python 3?

<1bmlqh-g5d5.ln1@esprimo.zbmc.eu>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=13929&group=comp.lang.python#13929

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: cl...@isbd.net (Chris Green)
Newsgroups: comp.lang.python
Subject: Re: How to iterate through maildir messages in python 3?
Date: Sun, 27 Jun 2021 08:35:29 +0100
Lines: 28
Message-ID: <1bmlqh-g5d5.ln1@esprimo.zbmc.eu>
References: <1m1fqh-32gb.ln1@esprimo.zbmc.eu> <ijklskF49sgU1@mid.individual.net> <56ggqh-v2de.ln1@esprimo.zbmc.eu> <7g0cdgpen22qgs1a805j3qshdpkmp2bqo7@4ax.com> <mailman.168.1624758047.4164.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net rKgU3v5nJ1JwRWajXXBvDAAYogV3c+5yrtfeKMmd4E7Xj4y8Y=
X-Orig-Path: not-for-mail
Cancel-Lock: sha1:bZpBSrIpQ38QgmFX5D2Tx2El6Oo=
User-Agent: tin/2.4.5-20201224 ("Glen Albyn") (Linux/5.11.0-22-generic (x86_64))
 by: Chris Green - Sun, 27 Jun 2021 07:35 UTC

Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote:
> On Fri, 25 Jun 2021 09:19:49 +0100, Chris Green <cl@isbd.net> declaimed the
> following:
>
> >
> >Here's the full program where I'm encountering the error (yes, I
> >should have posted this first time around) :-
> >
> > #!/usr/bin/python3
> >
> > import mailbox
> > import sys
> > import email
> >
> >
> > # open the existing maildir and the target mbox file
> > maildir = mailbox.Maildir(sys.argv [-2], email.message_from_file)
>
> Is there some reason you are overriding whatever the default "factory"
> is for Maildir?
>
Only that it was 'as copied' off the blog where I found it. As you
can see elsewhere in the thread that was exactly the problem, simply
removing the email.message_from_file fixed the error.

--
Chris Green
·


devel / comp.lang.python / Re: How to iterate through maildir messages in python 3?

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor