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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25977092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:23:26  来源:igfitidea点击:

Run bash script with python - TypeError: bufsize must be an integer

pythonbashsubprocess

提问by Or Smith

I'm trying to write python file, which wxtrac tar file in python.

我正在尝试编写python文件,该文件是python中的wxtrac tar文件。

As I understand, subprocessis the appropriate tool for this mission.

据我了解,subprocess是执行此任务的合适工具。

I write the following code:

我写了以下代码:

from subprocess import call


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


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

When output is the tar file, which located in /root/tryit/.

当输出是 tar 文件时,它位于/root/tryit/.

When I run it, i get the following message:

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

TypeError: bufsize must be an integer

Can I use tar command with this tool?

我可以在这个工具中使用 tar 命令吗?

回答by falsetru

You should specify the command as a list. Beside that, main option (x) is missing.

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

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

BTW, Python has a tarfilemodule:

顺便说一句,Python 有一个tarfile模块

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