mercredi 1 mars 2017

How to check the result of the bash test command inside a python script?

I would like to run the bash "test" command inside a python script.

For example, in the bash sell script, this can be done easily as follows. #!/bin/bash

if ! test -s ${file}; then
  echo "${file} does not have a positive size. "
  # Do some processing.. 
fi

With a python script, I think I may try the following way:

#!/usr/bin/python
import subrocess

try:
  subprocess.check_call("test -s " + file, shell=True)
except:
  print file + " does not have a positive size. "
  # Do some process  

Is the above approach a good way? If not, then could you please suggest an appropriate way?

Aucun commentaire:

Enregistrer un commentaire