Python: 子进程调用无法识别 * 通配符?
Python: subprocess call doesn't recognize * wildcard character?
我想删除文件中的所有 *.ts
。但是 os.remove
没有用。
>>> args = ['rm', '*.ts']
>>> p = subprocess.call(args)
rm: *.ts No such file or directory
rm
程序采用文件名列表,但 *.ts
不是文件名列表,它是匹配文件名的模式。您必须为 rm
命名实际文件。当您使用 shell 时,shell(但不是 rm
!)将为您扩展 *.ts
等模式。在Python中,你必须明确要求它。
import glob
import subprocess
subprocess.check_call(['rm', '--'] + glob.glob('*.ts'))
# ^^^^ this makes things much safer, by the way
当然有,何必subprocess
?
import glob
import os
for path in glob.glob('*.ts'):
os.remove(path)
我想删除文件中的所有 *.ts
。但是 os.remove
没有用。
>>> args = ['rm', '*.ts']
>>> p = subprocess.call(args)
rm: *.ts No such file or directory
rm
程序采用文件名列表,但 *.ts
不是文件名列表,它是匹配文件名的模式。您必须为 rm
命名实际文件。当您使用 shell 时,shell(但不是 rm
!)将为您扩展 *.ts
等模式。在Python中,你必须明确要求它。
import glob
import subprocess
subprocess.check_call(['rm', '--'] + glob.glob('*.ts'))
# ^^^^ this makes things much safer, by the way
当然有,何必subprocess
?
import glob
import os
for path in glob.glob('*.ts'):
os.remove(path)