mailnex.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:mailnex 作者: linsam 项目源码 文件源码
def attachFile(attachList, filename, pos=None, replace=False):
    """Check a path and add it to the attachment list
    If pos is given and replace is False, insert attachment at given position.
    If pos is given and replace is True, replace the attachment at the given position.
    """
    if pos is not None:
        if pos < 1 or pos > len(attachList):
            print("Bad position. {} not between 1 and {}".format(pos, len(attachList)))
            return
        # Adjust from human position to index
        pos -= 1
    try:
        st = os.stat(filename)
    except OSError as err:
        import errno
        # Can't read it. Is it because it doesn't exist?
        if err.errno == errno.ENOENT:
            print("WARNING: Given file doesn't currently exist. Adding to list anyway. We'll try reading it again when completing the message")
        else:
            print("WARNING: Couldn't get information about the file: %s" % err.strerror)
            print("Adding to list anyway. We'll try reading it again when completing the message.")
    else:
        if not os.access(filename, os.R_OK):
            print("WARNING: Can't read existing file. Adding to list anyway. We'll try again when completing the message.")
        else:
            print("Attachment added to list. Raw size is currently %i bytes. Note: we'll actually read the data when completing the message" % st.st_size)
            mtype = magic.from_file(filename, mime=True)
            print("Mime type appears to be %s" % mtype)
    if pos is None:
        attachList.append(filename)
    elif replace == False:
        attachList.insert(pos, filename)
    else:
        attachList[pos] = filename
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号