0

I am trying to check the md5sum of a software installed in my system. I will first explain the environment and then I will explain the problem. There is a directory /home/software. in this directory there multiple folders like software1, software2 and so on . in each of these folders there is a folder (say folder1) which contains a check*.md5 file(check123.md5). Its the contents of this file that I am trying to read.

Eg:

>>cd /home/software/software1
>>md5sum -c  folder1/check*.md5

When I try the above two commands in linux command line, I get the output. I am trying to write a script in python. So initially i used os.system and again it worked. But now my requirements are such that I have to use subprocess. But it does not work anymore.

My code can be simulated even in the python command line.

 import subprocess, os
 os.chdir(/home/software/software1)
 var = "md5sum -c  folder1/check*.md5"
 vars = shlex.split(var)
 p1 = subprocess.Popen(vars, stdout = open ("/tmp/test.txt", "a"))

The output is as follows: md5sum: folder1/check*.md5: No such file or directory.

Now I realise that this error comes only if I am not in the proper directory. But a os.cwd() showed that I am in the correct directory.

Does anyone know what the problem is?

Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
HighonH
  • 169
  • 3
  • 16
  • why not use python's native md5 methods? this is described here: http://stackoverflow.com/questions/16874598/how-do-i-calculate-the-md5-checksum-of-a-file-in-python – keda Nov 24 '15 at 19:46
  • 1
    http://stackoverflow.com/a/21565932/5087125 seems even better, in which someone actually benchmarks hashlib against running md5sum in a subprocess. It doesn't seem to be worth it and the built-in way is more portable to boot. – pvg Nov 24 '15 at 19:53

2 Answers2

2

The problem is that you are using a wildcard - * in your Popen command. You need to use shell=True in your Popen statement to execute the command through a shell interpreter and allow the use of the wildcards.

p1 = subprocess.Popen(vars, stdout = open ("/tmp/test.txt", "a"), shell=True)

That will get rid of the md5sum: folder1/check*.md5: No such file or directory error.

However, I think you should really be using Python's native methods described here

How do I calculate the md5 checksum of a file in Python?

or here

Generating an MD5 checksum of a file

Community
  • 1
  • 1
keda
  • 559
  • 2
  • 12
  • No I am not trying to calculate the md5sum of a file. Instead the .md5 file actually contains a list of md5sum of all the files in its folder. I am trying to check this content – HighonH Nov 24 '15 at 21:34
  • @HighonH so, read the contents of the file and process it using python. can you please post the contents of your file and a little more about what you are trying to do? i can try to help you. – keda Nov 24 '15 at 22:38
  • @HighonH also, `shell=True` is the way to fix your current issue. – keda Nov 24 '15 at 22:39
0

* wildcard is expanded by the shell on POSIX. subprocess.Popen does not start the shell by default.

There is no need to start a shell to read files matching a given file pattern in Python:

#!/usr/bin/env python
from glob import glob

for filename in glob('/home/software/software1/folder1/check*.md5'):
    with open(filename) as file:
        for line in file:
            process(line)

If you want to run a subprocess instead:

#!/usr/bin/env python
import subprocess
from glob import glob

subprocess.check_call(['md5sum'] + glob('*.md5'))
jfs
  • 399,953
  • 195
  • 994
  • 1,670