bash command not running through python

I have the following method:

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

        bash_command: command to be executed.
    """"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)

Which I use to run shell commands through Python. It seems to work for the majority of the cases except for this one:

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

Where self._prefix is a path such as /opt/annotations/. I'd expect this command to delete all txt files inside that path, which is not happening. However, if I run this command find \opt\annotations\ -name "*.txt" -type f -delete directly in the terminal, everything is erased as expected. So I'm wondering if I'm missing something here.

Read more here:

Content Attribution

This content was originally published by pceccon at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: