问题描述
我要运行的Python,它首先焦油文件的管道命令行的Linux / bash命令,然后分割tar文件。该命令看起来像这样在bash的内容:
I want to run a piped command line linux/bash command from Python, which first tars files, and then splits the tar file. The command would look like something this in bash:
> tar -cvf - path_to_archive/* | split -b 20m -d -a 5 - "archive.tar.split"
我知道我可以使用子进程执行它,通过设置壳= TRUE,并提交完整的命令作为一个字符串,像这样:
I know that I could execute it using subprocess, by settings shell=True, and submitting the whole command as a string, like so:
import subprocess
subprocess.call("tar -cvf - path_to_archive/* | split -b 20m -d -a 5 - 'archive.tar.split'", shell=True)
...但出于安全原因,我想找到一种方法来跳过壳= TRUE的一部分,(这需要一个字符串列表,而不是一个完整的命令行字符串,并不能处理管道字符正确)。有没有在Python这方面的任何解决方案?即,是否有可能建立联系管道不知何故,或一些其他的解决方案?
...but for security reasons I would like to find a way to skip the "shell=True" part, (which takes a list of strings rather than a full command line string, and which can not handle the pipe char correctly). Is there any solution for this in Python? I.e., is it possible to set up linked pipes somehow, or some other solution?
推荐答案
焦油可以拆分本身:
tar -L 1000000 -F name-script.sh cf split.tar largefile1 largefile2 ...
name-script.sh
#!/bin/bash
echo "${TAR_ARCHIVE/_part*.tar/}"_part"${TAR_VOLUME}".tar >&"${TAR_FD}"
要重新组装
tar -M -F name-script.sh cf split.tar
添加到您的Python程序。
Add this to your python program.
这篇关于以任何方式使用子模块,而不使用shell =真执行Python中的管道命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!