Python - How to invoke bash commands with pipe?

I can run this normally on the command line on Linux:

$ tar c my_dir | md5sum 

But when I try to call it using Python, I get an error:

 >>> subprocess.Popen(['tar','-c','my_dir','|','md5sum'],shell=True) <subprocess.Popen object at 0x26c0550> >>> tar: You must specify one of the `-Acdtrux' or `--test-label' options Try `tar --help' or `tar --usage' for more information. 
+8
python subprocess popen
source share
4 answers

You should use subprocess.PIPE , also to separate this command, you should use shlex.split() to prevent strange behavior in some cases:

 from subprocess import Popen, PIPE from shlex import split p1 = Popen(split("tar -c mydir"), stdout=PIPE) p2 = Popen(split("md5sum"), stdin=p1.stdout) 

But to create an archive and create its checksum, you must use the Python built-in tarfile and hashlib instead of calling shell commands.

+9
source share

Ok, I'm not sure why, but this works:

 subprocess.call("tar c my_dir | md5sum",shell=True) 

Does anyone know why the source code is not working?

+3
source share

Actually, you want to start a shell subprocess with a shell command as a parameter:

 >>> subprocess.Popen(['sh', '-c', 'echo hi | md5sum'], stdout=subprocess.PIPE).communicate() ('764efa883dda1e11db47671c4a3bbd9e -\n', None) 
+2
source share
 >>> from subprocess import Popen,PIPE >>> import hashlib >>> proc = Popen(['tar','-c','/etc/hosts'], stdout=PIPE) >>> stdout, stderr = proc.communicate() >>> hashlib.md5(stdout).hexdigest() 'a13061c76e2c9366282412f455460889' >>> 
+1
source share

All Articles