【问题标题】:Using tarfile library with special characters使用带有特殊字符的 tarfile 库
【发布时间】:2016-06-20 19:43:00
【问题描述】:

我想创建带有土耳其字符(如“ö”)的 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()

【问题讨论】:

    标签: python python-2.7 unicode tarfile


    【解决方案1】:

    在名称前使用“u”。

    foldername = u"klasör"
    

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

    【讨论】:

      猜你喜欢
      • 2018-07-02
      • 2014-10-15
      • 1970-01-01
      • 2021-11-02
      • 2023-03-14
      • 2012-10-06
      • 2018-11-14
      • 2012-09-15
      • 2017-03-15
      相关资源
      最近更新 更多