使用带有特殊字符的tarfile库

时间:2016-06-20 19:43:01

标签: python python-2.7 unicode tarfile

我想用土耳其语字符创建tarfile(比如“ö”),但是我收到错误。我在Windows 8.1上使用python 2.7。

这是我的代码:

# -*- coding: utf-8 -*-
import tarfile
import os
import sys

foldername = "klasör"

foldername = foldername.decode(sys.getfilesystemencoding())

tar = tarfile.open(foldername + ".tar.gz", "w:gz", compresslevel=5)
tar.add(foldername)
tar.close()

1 个答案:

答案 0 :(得分:2)

在名称之前使用“u”。

foldername = u"klasör"

您不需要对其进行编码/解码,而是将其保留为unicode并像您一样打开。