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

19.1.14 email: Examples - Issues with 5th example

我正在尝试从 .msg 文件中提取附件。 github 上的解决方案太详细了,我无法正确理解,这里的答案也没有我想要的那么完整。这个example在和我类似的另一个问题中被指出,但我一直无法弄清楚该功能是如何工作的。
我相信我很接近,但无法弄清楚我应该打开什么 - with open(msgFile) as 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')

关于我需要为函数提供的输入类型的任何输入或线索都将是一个巨大的帮助。谢谢

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)

然后,如果您需要此 'directory',请使用 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(或长选项名称)与值,默认值或您将从命令行获得的值。

'C:\Users\MikeS\Documents\ImproveBKFS\Python\msg_extract'和'TestFile.msg'看起来像一个目录和文件名。但是它们被用在 add_argumentdest 槽中。您可能需要花一些时间阅读 argparse 文档,并使用一些更简单的示例进行练习。