使用Python运行Bash脚本 - 类型错误:缓冲区大小必须是整数。

13

我想编写一个Python文件,用Python提取tar文件。

据我了解,subprocess是这个任务的合适工具。

我写下了以下代码:

from subprocess import call


def tarfile(path):
   call(["tar"], path)


if __name__ == "__main__":
    tarfile("/root/tryit/output.tar")

当输出为tar文件时,它位于/root/tryit/中。

运行时,我收到以下消息:

TypeError: bufsize must be an integer

我能用tar命令和这个工具一起使用吗?

1个回答

25

你应该将命令指定为列表。此外,主要选项(x)缺失。

def tarfile(path):
    call(["tar", "xvf", path])

顺便提一下,Python有一个tarfile模块

import tarfile
with tarfile.open("/root/tryit/output.tar") as tar:
    tar.extractall()  # OR tar.extractall("/path/to/extract")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接