ls
, as run through Python, is probably right: I guess there is no file called *CSV*APP*
in the current directory. There is probably a file with a name that matches that glob pattern. But ls
doesn't care about globs. What happens when you run the command on the shell, is that the shell expands the glob to matching filenames it can see in the current directory and these expanded names is what the shell passes to ls
.
To get the same result in the shell as in Python (for demonstration, not because you'd want that), protect the arguments from glob expansion by single quotes:
ls -lh '*CVS*APP*'${e}'.zip'
But how can you get the shell's behavior in Python? You can use shell=True
as some other answers suggest, but that is a slippery slope, as invoking an actual shell on your dynamically generated strings (maybe dependent on user input in a more complex application) can make you vulnerable to command injections and other nastiness.
Here, you only need one specific behavior of the shell, filename globbing. And Python happens to be able to do that all by itself:
import subprocess as sp
from glob import glob
sp.call(['cd', input_dir])
for i, e in enumerate(piv_id_list):
proc_out = sp.Popen(['ls', '-lh', glob('*CSV*APP*{0}.zip'.format(e))])
proc_out_list.append(proc_out)
print(proc_out)
As JuniorCompressor has pointed out, this would still look in the wrong directory, because the cd
would only affect the subprocess of the cd
invocation, so let's fix that, too:
import subprocess as sp
from glob import glob
os.chdir(input_dir)
for i, e in enumerate(piv_id_list):
proc_out = sp.Popen(['ls', '-lh', glob('*CSV*APP*{0}.zip'.format(e))])
proc_out_list.append(proc_out)
print(proc_out)
Note
You could probably use the slightly higher-level sp.check_output
instead of the underlying sp.Popen
directly.