tarfile.extractall()引发IsADirectoryError,因为提取路径存在

时间:2017-07-11 06:46:49

标签: python python-3.x tar

我无法提取生成的tar.gz文件,因为extractall()抱怨目标目录存在。但是,如果提取目录不存在,则只生成一个空文件。

我在网上找到的有关为tarfile.extractall()提取use no parameters的tar文件的所有示例(这意味着它尝试在同一目录中提取它并且IsADirectoryError失败了)或make sure to create the extraction path beforehand

这是使用Python 3.5.2。

复制脚本:

#!/usr/bin/python3

import os, tarfile, tempfile

# Create a test directory
test_dir = os.path.join(os.path.expanduser('~'), 'tarfile-test')
os.makedirs(test_dir, exist_ok=True)
os.chdir(test_dir)

# Create empty files to include in the tarfile
open('1.txt', 'a').close()
open('2.txt', 'a').close()
open('3.txt', 'a').close()

# Create the tarfile
compressed_file = 'packet.tgz'
with tarfile.open(compressed_file, 'w:gz') as tar:
    for f in os.listdir():
        tar.add(f, arcname=os.path.sep)

# Now attempt to extract it in three different places: a local directory, a
# temporary directory and a non-existent directory

# Local directory
local_dir = 'local-extraction'
os.makedirs(local_dir, exist_ok=True)
try:
    with tarfile.open(compressed_file, 'r:gz') as tar:
        tar.extractall(path=local_dir)
        print('Extracted in local dir!')
except IsADirectoryError:
    print('Failed to extract in local directory')

# Temporary directory
try:
    with tempfile.TemporaryDirectory() as tmp_dir:
        with tarfile.open(compressed_file, 'r:gz') as tar:
            tar.extractall(path=tmp_dir)
            print('Extracted in temporary dir!')
except IsADirectoryError:
    print('Failed to extract in temporary directory')

# Non-existent directory. This does not throw an exception, but fails to extract
# the files
non_existent = 'non_existent_dir'
with tarfile.open(compressed_file, 'r:gz') as tar:
    tar.extractall(path=non_existent)
    if os.path.isdir(non_existent):
        print('Extracted in previously non-existent dir!')
    else:
        print('Not extracted in non-existent dir')

输出:

$ ./repro.py 
Failed to extract in local directory
Failed to extract in temporary directory
Not extracted in non-existent dir

如果我们检查tarfile-test的内容:

$ ll
total 16
drwxrwxr-x  3 user user 4096 Jul 11 08:38 ./
drwxr-xr-x 31 user user 4096 Jul 11 08:38 ../
-rw-rw-r--  1 user user    0 Jul 11 08:38 1.txt
-rw-rw-r--  1 user user    0 Jul 11 08:38 2.txt
-rw-rw-r--  1 user user    0 Jul 11 08:38 3.txt
drwxrwxr-x  2 user user 4096 Jul 11 08:38 local-extraction/
-rw-rw-r--  1 user user    0 Jul 11 08:38 non_existent_dir
-rw-rw-r--  1 user user  124 Jul 11 08:38 packet.tgz

non_existent_dir是一个空文件,而不是目录。 local-extraction是空的。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

在创建tar.gz文件时,问题出现在arcname参数中。我(错误地)遵循了建议in this comment。但是,这应该只在打包目录时完成,它会破坏添加单个文件时使用的tar.gz文件。

更改/删除arcname中的tarfile.add()参数会修复它:

# Create the tarfile
compressed_file = 'packet.tgz'
with tarfile.open(compressed_file, 'w:gz') as tar:
    for f in os.listdir():
        tar.add(f)