使用Python 3.4处理希伯来文件和文件夹

时间:2016-01-06 10:18:58

标签: filesystems python-3.4 hebrew

我使用Python 3.4创建一个程序,通过电子邮件将特定附件保存到文件服务器。

根据发件人的电子邮件地址,每个文件都会保存到特定目的地。

我的问题是目标文件夹和附件都是希伯来语,对于一些附件我得到路径不存在的错误。 现在这是不可能的,因为它可能会失败一个附件但不能同一邮件上的其他附件(目标文件夹由发件人的地址决定)。

我想调试问题,但我无法让python显示它正在尝试正确保存的文件路径。 (它是混合的希伯来语和英语,它总是在一个大混乱中显示路径,虽然它在文件服务器保存到文件服务器的95%的时间内正常工作)

所以我的问题是: 我应该在这段代码中添加什么内容才能正确处理Hewbrew? 我应该编码或解码吗? 处理文件时我应该避免使用哪些字符?

这是失败的主要代码:

try:
    found_attachments = False
    for att in msg.Attachments:
        _, extension = split_filename(str(att))
        # check if attachment is not inline
        if str(att) not in msg.HTMLBody:
            if extension in database[sender][TYPES]:
                file = create_file(str(att), database[sender][PATH], database[sender][FORMAT], time_stamp)
                # This is where the program fails:
                att.SaveAsFile(file)
                print("Created:", file)
                found_attachments = True
    if found_attachments:
        items_processed.append(msg)
    else:
        items_no_att.append(msg)
except:
    print("Error with attachment: " + str(att) + " , in: " + str(msg))

和创建文件功能:

def create_file(att, location, format, timestamp):
    """
    process an attachment to make it a file
    :param att: the name of the attachment
    :param location: the path to the file
    :param format: the format of the file
    :param timestamp: the time and date the attachment was created
    :return: return the file created
    """

    # create the file by the given format
    if format == "":
        output_file = location + "\\" + att
    else:
        # split file to name and type
        filename, extension = split_filename(att)
        # extract and format the time sent on
        time = str(timestamp.time()).replace(":", ".")[:-3]
        # extract and format the date sent on
        day = str(timestamp.date())
        day = day[-2:] + day[4:-2] + day[:4]

        # initiate the output file
        output_file = format
        # add the original file name where needed
        output_file = output_file.replace(FILENAME, filename)
        # add the sent date where needed
        output_file = output_file.replace(DATE, day)
        # add the time sent where needed
        output_file = output_file.replace(TIME, time)

        # add the path and type
        output_file = location + "\\" + output_file + "." + extension
        print(output_file)
    # add an index to the file if necessary and return it
    index = get_file_index(output_file)
    if index:
        filename, extension = split_filename(output_file)
        return filename + "(" + str(index) + ")." + extension
    else:
        return output_file

在此先感谢,如果需要,我很乐意解释更多或提供更多代码。

1 个答案:

答案 0 :(得分:0)

我发现promlem没有使用希伯来语。我发现(路径+文件名)可以容纳的字符数限制(255个字符)。

失败的文件会触发该限制并导致问题