1

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.

Bach
  • 6,145
  • 7
  • 36
  • 61
pikapika
  • 95
  • 1
  • 2
  • 9

2 Answers2

0

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

Community
  • 1
  • 1
demux
  • 4,544
  • 2
  • 32
  • 56
0

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.

exhuma
  • 20,071
  • 12
  • 90
  • 123