panda0 panda0 - 8 months ago 34
Python Question

python call rm can't recognize * wildcard character?

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

can't work.
So,I try this.

But it can't recognize the


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'):