Doing the ls output magic in python

bash, linux, python, shell

Solution

The trick is to detect whether the output is a terminal, in which case `ls` uses columns, or not, in which case it outputs in a simpler format.

In Unix, you should be able to use Python's `os.isatty()` function to get this information.

From the shell, you can use the `tty(1)` program: `tty -s <&1`. This will exit true if stdout is a tty, and false if it is not. `tty` actually checks stdin, but with `<&1` you can redirect stdout to stdin to effectively check stdout.

Problem

I want to have the same output as ls creates in a bash or python script. I don't mean listing directories, but the thing ls does to be able to be "loopable". Example: ``` # ls a b c d # ls | head -n 1 a # for i in $(ls); do echo "file: $i"; done file: a file: b file: c file: d ``` How can ls be like that and still display everything in one line when calling it? Using tabs doesn't work.. Newlines just force it to be multiple lines. \000 doesn't work. ``` # echo -e "a\tb\tc\td" | head -n 1 a b c d echo -e "a\000b\000c\000d" | head -n 1 abcd ``` cat -A doesn't give me much info... ``` # cat -A <(ls --color=no) a$ b$ c$ d$ # cat -A <(echo -e "a\nb\nc\nd") a$ b$ c$ d$ ``` So.. How can I generate the same type of output does in my scripts? Is there any control characters I am missing here?

Original source