I have to execute a command and store the output of it in file. The output spans multiple pages and i have to press enter multiple times to see the complete output( similar to that when a man returns multiple pages). I am thinking of using the subprocess module, but how to provide input to the process, when the process prompts.
-
And which command would that be? – demux Feb 21 '14 at 07:04
-
1See http://stackoverflow.com/questions/8475290/how-do-i-write-to-a-python-subprocess-stdin – smci Feb 21 '14 at 07:06
-
No,I have to run -help on a utility, and it returns multiple pages. Got it running . Thanks. – pikapika Feb 21 '14 at 07:28
-
And how did you get it running? – demux Feb 22 '14 at 04:03
-
from here http://stackoverflow.com/questions/8475290/how-do-i-write-to-a-python-subprocess-stdin – pikapika Feb 23 '14 at 08:25
2 Answers
Disclaimer: I don't know which command you're actually executing so this is just a stab in the dark.
You should not have to provide any input.
Piping the output of the command to cat solves your problem:
less testfile.txt | cat
Also if your goal is to store the output in another file, you can simply to this (this will overwrite):
less testfile.txt > testfilecopy.txt
(and this will append):
less textfile.txt >> logfile.txt
See: https://unix.stackexchange.com/questions/15855/how-to-dump-a-man-page
The best solution is to check if the process does not support a command-line flag to run in "batch mode", disable paging or something similar which will suppress any such "waits". But I guess you have already done that. Given that you have to enter "-help" interactively tells me it's probably no standard unix command which are usually quite easy to run in a sub-process.
Your best bet in that case would be to use expect. There are python bindings available under pexpect.
Expect scripts tend to be fairly ugly, and error-prone. You have to be diligent with error handling. I have only limited practical experience with it as I only modified some of our existing scripts. I have not yet written one myself, but from our existing scripts I know they work, and they work reliably.

- 20,071
- 12
- 90
- 123