bash 命令不是 运行 通过 python

bash command not running through python

我有以下方法:

def _run_command(bash_command: str) -> str:
    """
    Run a bash command.

    Args:
        bash_command: command to be executed.
    """
    logging.info(f"Running {bash_command}.")
    process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
    output, error = process.communicate()
    if error is not None:
        logging.exception(f"Error running command: {bash_command}")
        raise ValueError(error)

    return str(output)

我通过 Python 使用 运行 shell 命令。它似乎适用于大多数情况,除了这个:

command = f'find {self._prefix} -name "*.txt" -type f -delete'
output = self._run_command(command)

其中self._prefix是一条路径,例如/opt/annotations/。我希望此命令会删除该路径内的所有 txt 文件,但这并没有发生。但是,如果我直接在终端中 运行 这个命令 find /opt/annotations/ -name "*.txt" -type f -delete ,所有内容都会按预期删除。所以我想知道我是否遗漏了什么。

日志记录显示 expect 命令,但是,txt 未被删除:

2020-11-18 19:07:47 fm-101 root[10] INFO Running find /opt/annotations/ -name "*.txt" -type f -delete.

问题出在引号上。您没有匹配 "*.txt" 的文件,只有匹配 *.txt.

的文件

将您的命令 明确地 作为列表传递,而不是尝试从字符串生成列表,以避免此问题和其他问题:

def _run_command(command_argv: list) -> str:
    logging.info(f"Running {command_argv}.")
    process = subprocess.Popen(command_argv, stdout=subprocess.PIPE)
    output, error = process.communicate()
    if error is not None:
        logging.exception(f"Error running command: {bash_command}")
        raise ValueError(error)
    return str(output)

_run_command(['find', str(self._prefix), '-name', '*.txt', '-type', 'f', '-delete'])

如果你坚持将参数作为字符串,使用shlex.split()得到POSIX sh-like(不是bash-like ) 引用值的处理:

def _run_command(shell_command: str) -> str:
    """
    Run a shell command, but without actually invoking any shell

    Args:
        shell_command: command to be executed.
    """
    logging.info(f"Running {shell_command}.")
    process = subprocess.Popen(shlex.split(shell_command), stdout=subprocess.PIPE)
    output, error = process.communicate()
    if error is not None:
        logging.exception(f"Error running command: {shell_command}")
        raise ValueError(error)

    return str(output)

...但这只能解决引用删除的具体问题;它不会使操作在任何其他方面 shell 兼容。