import subprocess
sources_path = "/usr/local/lib/python2.7/"
# Starting `ls` command to list all available
ls_files = subprocess.Popen(
["ls", "-1", sources_path],
stdout=subprocess.PIPE
)
# And now lets we're going to redirect `ls` command output to `wc` command
count_lines = subprocess.Popen(
["wc", "-l"],
stdin=ls_files.stdout,
stdout=subprocess.PIPE
)
ls_files.stdout.close()
lines_number, err = count_lines.communicate()
# Actually its equivalent to:
# $ ls -1 /usr/local/lib/python2.7/ | wc -l
print "Files in 2.7 python lib: {0}".format(lines_number)
print "Errors: {0}".format(err)