2017-11-04 3 views
-1

を使用して電子メールファイルを読み取ろうとする:私はフォルダディレクトリ内の電子メールファイルを読み込むしようとしているが、私はこのエラーを取得していパイソン

AttributeError: '_io.TextIOWrapper' object has no attribute 'message_from_file'

import os 
import email 

for dirName, subdirList, fileList in os.walk(directory): 
    print('Found directory: %s' % dirName) 
    for fname in fileList: 
     f = os.path.join(dirName, fname) 
     with open(f) as email: 
      msg = email.message_from_file(f) 
      print(msg['from']) 

これは、ファイルがどのように見えるかです:

Message-ID: <[email protected]> 
Date: Mon, 5 Nov 2001 07:08:06 -0800 (PST) 
From: [email protected] 
To: [email protected] 
Subject: vol book spreadsheets 
Mime-Version: 1.0 
Content-Type: text/plain; charset=us-ascii 
Content-Transfer-Encoding: 7bit 
X-From: Chen, Hai </O=ENRON/OU=NA/CN=RECIPIENTS/CN=HCHEN2> 
X-To: Arora, Harry </O=ENRON/OU=NA/CN=RECIPIENTS/CN=Harora> 
X-cc: 
X-bcc: 
X-Folder: \Harry_Arora_Jan2002\Arora, Harry\Inbox 
X-Origin: Arora-H 
X-FileName: harora (Non-Privileged).pst 
+0

何を電子メールで読むのですか? – mrid

+0

@gomb yea私は、更新されたOP – Batman

答えて

2

変数名を変更する必要があります。現在、email変数がemailモジュールへのモジュール参照をオーバーライドしています。これを試してみてください:

with open(f) as file: 
    msg = email.message_from_file(file) 
    print(msg['from']) 
+0

オハイオ州私はそれが意味を参照してください、それが働いているようだ – Batman

関連する問題