• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Linux Tips / How to Run a Shell Command from Python and Get The Output?

How to Run a Shell Command from Python and Get The Output?

March 27, 2014 by cmdlinetips

In Python, often you may want to execute linux command and get the output of the command as string variable. There are multiple ways to execute shell command and get output using Python. A naive way to do that is to execeute the linux command, save the output in file and parse the file.

import os
cmd = 'wc -l my_text_file.txt > out_file.txt'
os.system(cmd)

Get output from shell command using subprocess

A better way to get the output from executing a linux command in Python is to use Python module “subprocess”. Here is an example of using “subprocess” to count the number of lines in a file using “wc -l” linux command.

Let us first import the subprocess module

# import subprocess library
>import subprocess

Launch the shell command that we want to execute using subprocess.Popen function. The arguments to this command is the shell command as a list and specify output and error.

>out = subprocess.Popen(['wc', '-l', 'my_text_file.txt'], 
           stdout=subprocess.PIPE, 
           stderr=subprocess.STDOUT)

The output from subprocess.Popen is subprocess.Popen object. And this object has number of methods associated with it and we will be using communicate() method to get the standard output and error in case as a tuple.

Here standard output contains the result from wc -l command and the stderr contains None as there are no errors.

>stdout,stderr = out.communicate()
>print(stdout)
    3503 my_text_file.txt
>print(stderr)
None

We can then parse the stdout to get the result from shell command in Python, the way we want . For example, if we just want the number of lines in the file, we split the stdout

>stdout.split()[0]
'3503'

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow to read entire text file in Python? Default ThumbnailRunning Python Command Line and Getting Arguments Default ThumbnailUnderstanding Variables in Shell Scripting How Set PATH Variable on BASH Shell: Hint export commandHow To Set PATH variable on Bash Shell?

Filed Under: Linux Tips, Python, python subprocess get output, Python Tips Tagged With: Linux Command in Python, Linux Tips, Python Tips, Shell Command Output in Python, subprocess Python

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version