-1

My code requires that UFW (Uncomplicated firewall) is installed. The program will only be run on a Linux based OS. How would I go about having Python return a value if it finds UFW installed?

Edit: Instead of catching the output, I want to see if the folder (in the default installation path) exists.

  • 2
    How would you check if UFW is installed from the command line? Can you arrange to run the same commands using Python? – larsks Dec 16 '19 at 18:21
  • 1
    Does this answer your question? ['which' equivalent function in Python](https://stackoverflow.com/questions/5226958/which-equivalent-function-in-python) – manveti Dec 16 '19 at 18:25
  • I'm aware I could just run the command and catch the output, but I want to see if the folder (in the default installation path) exists or not. – Clemens Dotson Dec 16 '19 at 18:58
  • Possible duplicate of ['which' equivalent function in Python](https://stackoverflow.com/q/5226958/608639), [Test if executable exists in Python?](https://stackoverflow.com/q/377017/608639), [How to check if a program exists from a Bash script?](https://stackoverflow.com/q/592620/608639), etc. – jww Dec 16 '19 at 22:14

1 Answers1

1

Please note this is just an edited copy of links

  1. How do I check if directory exists in Python?
  2. Check if a program exists from a python script

Hope This helps

import subprocess
rc = subprocess.call(['which', 'ufw'])
if rc == 0:
    print('ufw installed!')
else:
    print('ufw missing in path!')

# To check whether path exists
import os

if os.path.exists("/usr/sbin/ufw") :
  print("path exists")
else:
  print("path doesn't exist") 
Akhil Suresh
  • 151
  • 9