3

I want to extract tarfile to it's own path but if the file's path contains any special characters(like ö).

If I don't give extracting path, it extracts tarfile to my project directory.

Here is my code:

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

filepath = u"C:\\Users\\Ayse\\Desktop\\somefolderö\\tarfile.tar.gz"
dirname = os.path.dirname(os.path.realpath(filepath))
dirname = dirname.encode('utf-8')

tar = tarfile.open(filepath, "r:gz")
tar.extractall(path=dirname) #error occurs on this line
tar.close()

I get this error on tar.extractall(path=dirname) line:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xf6 in position 4: ordinal not in range(128)

I tried to encode the path variable like this:

filepath = filepath.encode('utf-8')

After encoding, code works without error. However, tarfile will be extracted to somefolderö folder. I want to extract it to somefolderö.

Long story short, ö becomes ö.

I'm using python 2.7 on Windows 8.1.

Ayse
  • 43
  • 1
  • 5

0 Answers0