0

I still need to use the file after it is parsed as en email but the email parser is closing it.

What can I do?

thanks

(venv3.4)ubuntu@core01:~/tmp$ cat tmp.eml
From: Example Person <[email protected]>
To: [email protected]
Subject: test2
Date: Sun, 2 Mar 2014 15:42:27 +1100

Hello

(venv3.4)ubuntu@core01:~/tmp$ cat tmp.py

from email.parser import BytesParser, BytesHeaderParser
from email import policy

f = open('tmp.eml', 'rb')

def parsefromfile(f, headersonly=None):
    f.seek(0)
    if headersonly:
        msg = BytesHeaderParser(policy=policy.default).parse(f)
    else:
        msg = BytesParser(policy=policy.default).parse(f)
    print(msg)
    print(msg.get('date', None))
    f.seek(0)
    print(f.read())

parsefromfile(f)


(venv3.4)ubuntu@core01:~/tmp$ python tmp.py

From: Example Person <[email protected]>
To: [email protected]
Subject: test2
Date: Sun, 2 Mar 2014 15:42:27 +1100

Hello


Sun, 02 Mar 2014 15:42:27 +1100
Traceback (most recent call last):
  File "tmp.py", line 17, in <module>
    parsefromfile(f)
  File "tmp.py", line 14, in parsefromfile
    f.seek(0)
ValueError: seek of closed file
6
  • This is quite strange because the source code for parse is pretty simple hg.python.org/releasing/3.4/file/77082b818676/Lib/email/… and it doesn't do anything with fp except read. Commented May 12, 2014 at 0:55
  • @luk32 are you saying it doesn't close the file? How can the file be closed if the parser doesn't close it? Commented May 12, 2014 at 1:01
  • I dunno, check the code it's under 10 lines only, really simple. It creates a parser object, and reads data from a file pointer, but it doesn't close it. I also think you can pass headersonly as the argument and spare this one if. Commented May 12, 2014 at 1:03
  • Um... there is inconsistency in the behaviour IMO. Because if you open file in the text mode and use Parser in the text mode it doesn't close the file. But BytesParser overwrites fp with TextIOWrapper and it somewhat closes it. I think it can be considered a bug. Commented May 12, 2014 at 1:25
  • A somewhat closed file is certainly not a good thing. Commented May 12, 2014 at 1:32

2 Answers 2

1

Use the parsebytes function instead. Get the string content of the file using .read() and pass that in, then continue operating on your file object.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes this makes sense don't know why I didn't think of it. Does anyone know why the parser closes the file?
1

So, this was actually a bug in python - http://bugs.python.org/issue21476. The fix is set to go live, I think, on python3.5 and next minor version of python 3.4 i.e. 3.4.2. The file descriptor should not get closed, and OPs code should be valid for those versions.

1 Comment

Nice one on the follow up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.