19.1.14电子邮件:示例 - 第5个示例的问题

时间:2017-03-17 22:11:19

标签: python python-3.x email argparse

我正在尝试从.msg文件中提取附件。 github上的解决方案太详细,我无法正确理解,这里的答案并不像我想的那么完整。在另一个与我类似的问题中指出了example,但我无法弄清楚该功能是如何工作的。
我相信我很接近,但无法弄清楚它是什么我打开 - 用open(msgFile)作为fp:这是我的代码到目前为止:

import os
import sys
import email
import errno
import mimetypes

from argparse import ArgumentParser

def main():
    parser = ArgumentParser(description = """\
Unpack a MIME message into a directory of files.
""")
    parser.add_argument('-d', '--    C:\\Users\\MikeS\\Documents\\ImproveBKFS\\Python\\msg_extract', required = True, 
                    help = """Unpack the MIME message into the named 
                    directory, which will be created if it doesn't already
                    exist.""")
parser.add_argument('TestFile.msg')
args = parser.parse_args()

with open(args, 'wb') as fp:  #issues with open(args)
    msg = email.message_from_file(fp)

try:
    os.mkdir(args.directory)
except FileExistsError:
    pass

counter = 1
for part in msg.walk():
#multipar/* are just containers
    if part.get_content_maintype()=='multipart':
        continue
    #Applications should really sanitize the given filename so that an
    # email message can't be used to overwrite important files
    filename = part.get_filename()
    if not filename:
        ext = mimetypes.guess_extension(part.get_content_type())
        if not ext:
            #Use a generic bag-of-bit extension
            ext = '.bin'
        filename = 'part-%03d%' % (counter, ext)
    counter +=1
    with open(os.path.join(arg.directory, filename), 'wb') as fp:
        fp.write(part.get_payload(decode=True))

if __name__ == '__main__':
    main()

我被困在第20行。这是我的输入和错误消息。

PS C:\Users\MikeS\Documents\\Python\msg_extract> py -3       get_payload_3.py -d ^V TestFile.msg
Namespace(C:\Users\MikeS\Documents\Python\msg_extract='\x16',   TestFile.msg='TestFile.msg')
Traceback (most recent call last):
  File "get_payload_3.py", line 53, in <module>
    main()
  File "get_payload_3.py", line 26, in main
    with open(args) as fp:  #something with this line
TypeError: invalid file:     Namespace(C:\Users\MikeS\Documents\Python\msg_extract='\x16',     TestFile.msg='Test
File.msg')

关于我需要提供该功能的输入类型的任何输入或线索将是一个巨大的帮助。感谢

1 个答案:

答案 0 :(得分:0)

parser.add_argument('-d', '--    C:\\Users\\MikeS\\Documents\\ImproveBKFS\\Python\\msg_extract', required = True, 
                help = """Unpack the MIME message into the named 
                directory, which will be created if it doesn't already
                exist.""")

该字符串的用途是什么:'-- C:\\Users\\MikeS\\Documents\\ImproveBKFS\\Python\\msg_extract'

使用简单的内容,例如--dir

parser.add_argument('-d', '--directory', required = True, 
                help = """Unpack the MIME message into the named 
                directory, which will be created if it doesn't already
                exist.""")

解析args后,打印它以便了解解析器发现的内容:

args = parser.parse_args()
print(args)

然后,如果您需要此目录&#39;,请使用args.directory之类的内容。

也改变了这一点:

parser.add_argument('TestFile.msg')

parser.add_argument('outfile')

并使用:

with open(args.outfile, 'wb') as fp:  #issues with open(args)
msg = email.message_from_file(fp)

我怀疑您将参数的dest(或长选项名称)与值混淆,无论是默认值还是您从命令行获取的值。

&#39; C:\用户\ MIKES \文件\ ImproveBKFS \的Python \ msg_extract&#39;和&#39; TestFile.msg&#39;看起来像一个目录和文件名。但它们正在dest的{​​{1}}位置使用。您可能需要花一些时间阅读add_argument文档,并使用一些更简单的示例进行练习。