I\'m trying to get a list of the CSV files in a directory with python. This is really easy within unix:
ls -l *.csv
And, predictably, I get
p=subprocess.Popen(["ls", "-l", "*.out"], stdout = subprocess.PIPE, shell=True)
causes
/bin/sh -c ls -l *.out
to be executed.
If you try this command in a directory, you'll see -- in typical mystifying-shell fashion -- all files are listed. And the -l flag is ignored as well. That's a clue.
You see, the -c flag is picking up only the ls. The rest of the arguments are being eaten up by /bin/sh, not by ls.
To get this command to work right at the terminal, you have to type
/bin/sh -c "ls -l *.out"
Now /bin/sh sees the full command "ls -l *.out" as the argument to the -c flag.
So to get this to work out right using subprocess.Popen, you are best off just passing the command as a single string
p=subprocess.Popen("ls -l *.out", stdout = subprocess.PIPE, shell=True)
output,error=p.communicate()
print(output)