panda0 panda0 - 2 months ago 9
Bash Question

Python: subprocess call doesn't recognize * wildcard character?

I want to remove all the *.ts in file.

didn't work.

And this doesn't expand

>>> args = ['rm', '*.ts']
>>> p =
rm: *.ts No such file or directory


The rm program takes a list of filenames, but *.ts isn't a list of filenames, it's a pattern for matching filenames. You have to name the actual files for rm. When you use a shell, the shell (but not rm!) will expand patterns like *.ts for you. In Python, you have to explicitly ask for it.

import glob
import subprocess
subprocess.check_call(['rm', '--'] + glob.glob('*.ts'))
#                            ^^^^ this makes things much safer, by the way

Of course, why bother with subprocess?

import glob
import os
for path in glob.glob('*.ts'):