我想在以下位置解压缩一堆文件:
/users/xyz/desktop/abc.tar.gz
/users/xyz/desktop/def.tar.gz
/users/xyz/desktop/hij.tar.gz
以下:
/users/xyz/desktop/abc
/users/xyz/desktop/def
/users/xyz/desktop/hij
基本上我试图保持untarred内容的目录名与原始文件名相同。我正在对以下功能进行修改,但它会保留桌面上的所有内容,而不是像我上面提到的那样创建单独的文件夹。我在这里缺少什么?
def untar(paths):
total = 0
for path in paths:
if os.path.isdir(path):
try:
dir_list = os.listdir(path)
except:
pass
else:
total += untar(os.path.join(path, new) for new in dir_list)
elif os.path.isfile(path):
try:
tarfile.open(path).extractall(os.path.dirname(os.path.join(os.path.dirname(path), os.path.basename((path)))))
except:
pass
else:
total += 1
return total
答案 0 :(得分:0)
问题来自那段代码:
template <typename ...Args>
struct PublisherContainer
{
std::tuple<Publisher<Args>...> publisher;
};
并且存在问题,因为
os.path.dirname(os.path.join(os.path.dirname(path), os.path.basename(path)))
你应该使用它:
os.path.join(os.path.dirname(path), os.path.basename(path))==path
也许你必须首先创建目录import re
#get the archive name and replace tar.gz, or tar.bz2 or tar.wtf
#by nothing
archive_name= re.sub(r"\.tar(\.[a-zA-Z0-9]{2,3})", "", os.path.basename(path))
output = os.path.join(os.path.dirname(path),archive_name)
#then we extract
tarfile.open(path).extractall(output)
,不确定......