Python equivalent to @lines = 'command';

Fernando Pérez fperez528 at yahoo.com
Mon Dec 17 06:07:44 EST 2001


Kirill Miazine wrote:

> * Christer Frovik <013-139047 at telia.com> [20011218 14:09]:
>> Is is possible to spawn a command (any command not just ls)
>> and have all the lines that the command outputs stored in a list?
>> 
>> In PERL this is done quite nicely in a single statement:
>> 
>>   @files = `ls -1`;
>> 
>> and you can then iterate over @files and do whatever you want.
>> Any ideas, or should i stick with Perl for my mindless scripts?
> 
> import os
> files = os.popen("ls -1").readlines()
> 

could anyone comment on the benefits/differences between the popen() approach 
and using
        output = commands.getoutput(cmd) ?

Or is getoutput() just a popen() call with another name?

thanks,

f.



More information about the Python-list mailing list