newbie question - executing system commands

Alex Martelli aleax at aleax.it
Tue Sep 4 10:21:35 EDT 2001


"Borzoj" <borzoj at pingu.ii.uj.edu.pl> wrote in message
news:9n23h1$qsc$1 at news.formus.pl...
> I'm newbie both in shell scripting and pyhon so please be kind :)
> What would be the Python equivalent for following shell code:
>
> for IMAGE_NAME in `ls`; do {
> #processing of $IMAGE_NAME
> };done
>
>
> ...or in other words what is the easiest way to execute shell
> command/external program from python and
> get access to it's output?

For this specific example, you want os.popen:

    import os

    for image_name in os.popen('ls').readlines():
        process(image_name)


Alex






More information about the Python-list mailing list