0

I'm searching a way to delete part of the output generated by script in stdout. We suppose that the output is the following:

line 1: some text...
line 2: other text...
line 3: and so on...

At this point of the script I call os.system("ls") and this generate a list of files in current directory with an amount of output that I'm not able to know in advance. So, the stdout will update as

line 1: some text...
line 2: other text...
line 3: and so on...
line 4: file1
line 5: file 2
...
line n: file n

I would like to clean stdout by line 3 to line n but I don't know how to do it. I've already tried to save cursor position at line 3 using escape \033[s (and then delete line by line using \033[K escape) but this doesn't work (my system doesn't support \033[s). I'm able to return at line 3 using escape \033[<L>;<C>H but I don't understand how to remove text until the end (or remove lines backward until line 3). Someone have an idea how to do it? Any suggestion is appreciated. Thanks

1
  • You are trying to move output back up? ie printout line 1, line 2, line 3, then go back up to line 1 and print out something else? That doesn't work on windows and only certain terminals support special characters to actually do this. you can use \b on windows to go back on the same line but you can't go backwards. An alternative on windows is clearing the whole screen, but this usually just prints out \n characters instead of actually clearing the terminal output. Windows does provide its own API for advanced functionality as well but it isn't cross platform. Commented May 1, 2017 at 15:48

1 Answer 1

1

Instead of fiddling with the terminal, you could capture stdout from the called process, print the first three lines to your own stdout and then either ignore further output or just kill the process. If it is really just ls you are executing, you may find the glob-module handy.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm not sure to understand your advice. In this way the output of ls is "hidden". In this case this is not my solution. I have to display the content of directory because the user must have the possibility to choose one file. After that (all line with list directory are garbage) the line 4 show the file choosen and the script go on with other output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.