One of my favorite features in perl is backtics: They execute something on the command line, and load the output as a variable:
> @config_filenames = `ls *.config`; # loads all the config files in this directory
Easy, huh? Here's the equivalent in python. Not as short, but it works:
include commands
dothis = "ls *.config"
temp = commands.getoutput(dothis)
Cool, I didn't know about the commands module. However, it's been deprecated.
ReplyDeleteThe equivalent with subprocess:
import subprocess # note- not include!
dothis = 'ls *.config'
temp = subprocess.check_output(dothis.split())
but it gives nastier error messages when it fails, I think
Update: Need to have "shell=True" if there's an asterisk:
ReplyDeletetemp = subprocess.check_output(dothis, shell=True).split()