python - 使用 python 运行 bash 脚本 - TypeError : bufsize must be an integer

标签 python bash subprocess

我正在尝试编写 python 文件,即 python 中的 wxtrac 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 命令吗?

最佳答案

您应该将命令指定为列表。除此之外,缺少主要选项 (x)。

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

顺便说一句,Python 有一个 tarfile module :

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

关于python - 使用 python 运行 bash 脚本 - TypeError : bufsize must be an integer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25977092/

相关文章:

python - Pandas Dataframe 选择多个不连续的列/切片

python - 在 PyQt 中绘制多边形

regex - 如何使用sed替换字符串中间的匹配字符

linux - 如何在 bash 脚本中使用 awk 过滤 2 个日期之间的数据

javascript - 有没有办法使用像 React 这样的前端框架在浏览器中运行 Python 脚本客户端?

Python For 循环、函数问题

linux - 为什么我们或 || Bash 中是否有任何条件为真,如 "iw dev interface_name del || true"?

python - 杀死通过 sudo 启动的子进程

python - 在 Windows 10/Pycharm 中使用 Vagrant 从 Python 调用 FastDownward planner

python - subprocess.Popen 实例的运行时间