Skip to content

Instantly share code, notes, and snippets.

@datavudeja
Forked from timothymillar/read_shell.py
Created September 14, 2025 18:34
Show Gist options
  • Save datavudeja/9b9d575fe27b34546966d246eae96f44 to your computer and use it in GitHub Desktop.
Save datavudeja/9b9d575fe27b34546966d246eae96f44 to your computer and use it in GitHub Desktop.
Read the result of a shell command into a pandas dataframe
#! /usr/bin/env python3
import io
import subprocess
import pandas
def read_shell(command, **kwargs):
"""
Takes a shell command as a string and and reads the result into a Pandas DataFrame.
Additional keyword arguments are passed through to `pandas.read_table`.
:param command: a shell command that returns tabular data
:type command: str
:return: a pandas datafram
:rtype: :class:`pandas.dataframe`
"""
proc = subprocess.Popen(command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output, error = proc.communicate()
if proc.returncode == 0:
with io.StringIO(output.decode()) as buffer:
return pandas.read_table(buffer, **kwargs)
else:
message = ("Shell command returned non-zero exit status: {0}\n\n"
"Command was:\n{1}\n\n"
"Standard error was:\n{2}")
raise IOError(message.format(proc.returncode, command, error.decode()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment