I have the following command that I want to execute in Python and store in a variable:
ls -1 var1 2>/dev/null | grep var2 | grep var3 | head -n 1
For executing shell commands you'd use the subprocess module.
Usage and examples can be found at: Python Docs: subprocess
The actual python code for calling bash would look like this
import subprocess task = subprocess.Popen("ls -1 var1 2>/dev/null | grep var2 | grep var3 | head -n 1", shell=True, stdout=subprocess.PIPE) directory = task.stdout.read() print(directory) # result
The recommended way though would to use python to do the directory search. Python listdir() The command for oslistdir could look like this
files = [f for f in os.listdir('.') if re.match(r'[0-9]+.*\.jpg', f)]