I'm writing an automation script, where it needs to run a command and the output of command should be captured as a list.
For example:
# ls -l | awk '{print $9}'
test1
test2
I want the output to be captured as a list like var = ["test1", "test2"].
Right now I tried this but it is saving as string instead of list:
# Filter the tungsten services
s = subprocess.Popen(["ls -l | awk '{print $9}'"], shell=True, stdout=subprocess.PIPE).stdout
service_state = s.read()
Please guide me if anyone has any idea to achieve this.
s.read().splitlines()shlex.split()on your command string; unless you want some bugs in your software.ls -lif you don't need to mywiki.wooledge.org/ParsingLs. You can use globbing with*to expand all files and directories in the current directory. e.g.echo *