subprocess.check_output(cmd) 不针对 tar -C 选项执行

subprocess.check_output(cmd) doesn't execute for tar -C option

我正在尝试使用 subprocess.check_output() 运行 以下命令。如果 运行 直接在 bash 中,命令(如下所示)工作正常:

tar -C /tmp/models/  -czvf model.tar.gz .

如果我在通过子进程 运行 时不使用“C”选项,它也 运行 没问题。

cmd = ['tar', 'czf', "/tmp/model.tar.gz", "/tmp/models/"]
output = subprocess.check_output(cmd).decode("utf-8").strip() # Works

但是当我尝试将 -C 选项与上述 tar 命令一起使用时,出现异常 tar: Must specify one of -c, -r, -t, -u, -x.

cmd = ['tar', 'C', '/tmp/models', 'cf', 'model.tar.gz', '.'] # fails. Other variations of this fail too.

如何 运行 上述 tar 命令正确使用子进程?谢谢。

我正在使用 python3.8

看起来应该指定破折号 -:

$ tar C whatever czvf thing.tar .
tar: Must specify one of -c, -r, -t, -u, -x

$ tar C whatever -czvf thing.tar .
tar: could not chdir to 'whatever'

所以命令应该是这样的:

cmd = ['tar', '-C', '/tmp/models', '-cf', 'model.tar.gz', '.']