I'm having trouble understanding how to use subprocess
for a problem on mine.
Let's say I have a tab-delimited text file tabdelimited1.txt
in my subdiretory which I would like to read into a pandas dataframe.
Naturally, you could simply import the data as follows:
import pandas as pd
df = pd.read_csv("tabdelimited1.txt", header=None, sep="\s+")
However, let's say we wanted to use subprocess
. In the command line, $cat tabdelimited1.txt
will output all of the lines.
Now, I want to use subprocess to read the output of cat tabdelimited1.txt
. How does one do this?
We could use
import subprocess
task = subprocess.Popen("cat file.txt", shell=True, stdout=subprocess.PIPE)
data = task.stdout.read()
but (1) I get an error for shell=True
and (2) I would like to read in the data line-by-line.
How can I use subprocess
to read tabdelimited1.txt
line-by-line? The script should look something like this:
import subprocess
import pandas as pd
df = pd.DataFrame()
task = subprocess.Popen("cat file.txt", shell=True, stdout=subprocess.PIPE)
# while lines exist:
# line = subprocess std
df=pd.concat([df, line])
EDITED