0

I am trying to capture all the data that I get from a python script that I run into a variable. I took a few inputs from the user within the script and I want to capture everything from start till the end.

It is almost similar to this : Running shell command and capturing the output

But again this uses the subprocess within the script to get an output.

What I want is something like this:

when i run ls -l in the terminal, I want my script to capture the ouput of ls -l

If i write :

p2 = subprocess.Popen('ls' ,'-l',stdout= subprocess.PIPE).communicate()[0])

within my script, that will execute the script twice.

The expected output is to capture all the data when i run ls -l in the terminal to be captured in p2.

Moh
  • 91
  • 2
  • 7

2 Answers2

0

If you want to avoid all the hassle of using subprocess.Popen() explicitly, just go for os.popen("command").read() - it runs the former under the hood, and in my case the result of p2 = os.popen("ls -l").read() looks just right.

0

You can use pexpect to easily open a PTY and log everything it shows:

#!/usr/bin/env python3
import pexpect
import io

print("-- Running the program")
p = pexpect.spawn("./foo.py")
p.logfile_read = io.BytesIO()
p.interact();

print("-- Program exited. Here is the log:")
log = p.logfile_read.getvalue().decode("utf-8")
print(log)

Here's a foo.py:

#!/usr/bin/env python3
name=input("Enter name: ")
print("Hello " + name)

Here's what happens when you run it from a terminal:

$ ./foo.py
Enter name: World
Hello World

And here's what happens when you run it from the logging script:

$ ./log.py
-- Running the program
Enter name: World
Hello World
-- Program exited. Here is the log:
Enter name: World
Hello World
that other guy
  • 116,971
  • 11
  • 170
  • 194
  • That is interesting. I am also using arguments with the script. It keeps changing the way the user wants a certain thing done. How can I take that into consideration using pexpect? – Moh Oct 23 '19 at 00:41
  • You can pass a list `args=['-a', '-b']` to `spawn` – that other guy Oct 23 '19 at 00:43
  • I am using argparse for arguments in my scripts. Some of the arguments does not have a value and some does. How will args list in spawn change for args with values? – Moh Oct 31 '19 at 03:03
  • It's up to you to know which arguments your program expects. First figure out how to invoke it manually on a command line, then you can try to invoke it via pexpect. – that other guy Oct 31 '19 at 05:49
  • So I am assuming the log script and the actual script you want to log should be different. I wanted a snapshot of what foo.py does within foo.py itself. Tl;dr I am recording what the script is doing inside a variable within foo.py itself. – Moh Nov 02 '19 at 23:24
  • In that case you could just write the script so that any time you read or write something, it's also added to a log – that other guy Nov 03 '19 at 03:03
  • That is what I want and I am not sure how will I be able to do that. Still a novice in Python:) – Moh Nov 03 '19 at 05:01