Athena
Athena

Reputation: 3228

Python 2.7 subprocess returns nothing, exact command works in console

I'm trying to execute the strings command inside of a Python script. I had it working for awhile, but mysteriously, it doesn't return anything when I use subprocess.Popen anymore.

Called from terminal

strings /path/to/.bashrc
# You may uncomment the following lines if you want `ls' to be colorized:
# export LS_OPTIONS='--color=auto'
# eval `dircolors`
# alias ls='ls $LS_OPTIONS'
# alias ll='ls $LS_OPTIONS -l'
# alias l='ls $LS_OPTIONS -lA'
# Some more alias to avoid making mistakes:
# alias rm='rm -i'
# alias cp='cp -i'
# alias mv='mv -i'
shopt -u -o history

In Python shell:

import subprocess
proc = subprocess.Popen(['strings', '/path/to/.bashrc'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = proc.communicate()

The second returns nothing. Why?

Upvotes: 0

Views: 338

Answers (1)

ynimous
ynimous

Reputation: 5162

From the subprocess documentation:

The shell argument (which defaults to False) specifies whether to use the shell as the program to execute. If shell is True, it is recommended to pass args as a string rather than as a sequence.

Try using:

proc = subprocess.Popen("strings /path/to/file", ...)

Upvotes: 2

Related Questions