On Aug 25, 6:16 am, Nobody <nob...@nowhere.com> wrote: > On Tue, 25 Aug 2009 01:36:08 -0700, nickname wrote: > > I am a relative newbie to python, I am using os.popen to run an > > ls command. The output that I get using the read() function is > > different in look and feel from when I run the ls command natively > > from the shell (not via python). > > As others have pointed out, the default behaviour of ls is different if > its output is a terminal. > > > Is there an easy way to "mirror" the output. When python displays the > > output, how can it tell the bash shell that some of the entries are > > directories and they should appear blue on the bash shell, and that > > everything should not be appearing on 1 column only. > > You can get the terminal-style behaviour even when using a pipe with: > > ls -x --color > > But why are you reading this information into Python then writing it > back out to the terminal? > > If you're planning on processing the output within Python, both the > multi-column format and the escape sequences used for colour will make > such processing awkward. > > If you want to enumerate the contents of a directory within Python, use > os.listdir(). > > If you want to generate coloured output, use the curses module, e.g.: > > #!/usr/bin/env python > > import sys > import curses > > curses.setupterm() > setaf = curses.tigetstr('setaf') or "" > setab = curses.tigetstr('setab') or "" > origp = curses.tigetstr('op') or "" > > def fg(c): > sys.stdout.write(curses.tparm(setaf, c)) > > def bg(c): > sys.stdout.write(curses.tparm(setab, c)) > > def orig(): > sys.stdout.write(origp) > > # example > bg(curses.COLOR_BLUE) > fg(curses.COLOR_YELLOW) > print "hello, world" > orig()
wow guys! thanks for all the great leads! this is awesome! The reason why I want to do this is because I am going to do a little project. I will write a python script called ls which will log the time and username and then will show the actual ls output. I want this to be transparent and so want to throw the ls output (via python) exactly as it will be in native shell execution. I know there's history files I can look up, but I just am exploring my own intermediate-logging-layer the functionality for which is executed right before the actual command is executed. Thanks, -A -- http://mail.python.org/mailman/listinfo/python-list