Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关于Python-imaplib库中方法append的问题_Python_Datetime_Imap_Imaplib - Fatal编程技术网

关于Python-imaplib库中方法append的问题

关于Python-imaplib库中方法append的问题,python,datetime,imap,imaplib,Python,Datetime,Imap,Imaplib,我正在尝试编写一个imapsync软件,将主机1连接到account1@host1.com并将邮件和文件夹复制到account2@host2.com2号旅馆 假设我已经用他的UID获取了所选邮件: msg = connection.fetch(idOne, '(RFC822)')) msg是一条很好的消息,下面是我尝试附加消息的代码: date = connection.fetch(idOne, '(INTERNALDATE)')[1][0] date = date.split("\"")[1

我正在尝试编写一个imapsync软件,将主机1连接到account1@host1.com并将邮件和文件夹复制到account2@host2.com2号旅馆

假设我已经用他的UID获取了所选邮件:

msg = connection.fetch(idOne, '(RFC822)'))
msg是一条很好的消息,下面是我尝试附加消息的代码:

date = connection.fetch(idOne, '(INTERNALDATE)')[1][0]
date = date.split("\"")[1]
authconnection1.append(folder, "", date, msg)
错误是:

ValueError: date_time not of a known type
我尝试过许多其他可能的解决方案(使用dateutil将日期字符串转换为datetime对象,使用imaplib.Time2Internaldate,我在ValueError上面遇到了相同的错误:date\u time不是已知的类型),但似乎没有一个可行。我在网络上搜索过,但似乎没有人有这个问题

有什么想法吗?我对此感到非常沮丧

多谢各位

更新:

我已经解决了日期时间问题,因为imaplib的“append”方法希望日期时间是秒的整数,所以要检索我编写的代码中的日期:

# Fetch message from host1
msg = connection.fetch(idOne, '(RFC822)')
# retrieve this message internal date
date = connection.fetch(idOne, '(INTERNALDATE)')[1][0].split("\"")[1]
# Cast str date to datetime object
date = parser.parse(date)
# Removes tzinfo
date = date.replace(tzinfo=None)
# Calculates total seconds between today and the message internal date
date = (datetime.datetime.now()-date).total_seconds()
# Makes the append of the message
authconnection1.append(folder, "", date, msg)
但现在它失败了,出现了错误:

TypeError: expected string or buffer
所以问题只是改变了

有什么想法吗

更新(已解决):

imaplib工作不正常,所以我为附加具有正确日期/时间的消息做了一个变通方法。这是我的代码,我希望它能帮助大家:

将日期转换为正确格式的函数:

def convertDate(date):
    from time import struct_time
    import datetime
    from dateutil import parser

    date = parser.parse(date)
    date = date.timetuple()
    return date
主要代码:

#Get message
msg = authconnection.fetch(idOne, '(RFC822)')
#Get message date
date = authconnection.fetch(idOne, '(INTERNALDATE)')[1][0].split("\"")[1]
#Apply my function I've written above
date = convertDate(date)
#Append message with right date
authconnection1.append(folder, flags, date, msg[1][0][1])

Python的哪个版本?2.7,但我正在更新这个问题。