Python equivalent to @lines = 'command';
Tim Hammerquist
tim at vegeta.ath.cx
Tue Dec 18 09:32:58 EST 2001
Christer Frovik <013-139047 at telia.com> graced us by uttering:
> 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?
Not familiar with PERL, but I'm quite familiar with Perl.
You seem to be looking to pipe a command's output to a variable
(which is all Perl's backticks really do). Look at the os.popen family
of functions, in this case, os.popen()
import os
lines = os.popen('ls -1').readlines()
for line in lines:
process(line)
or
import os
pipe = os.popen('ls -1')
for line in pipe.readlines():
process(line)
Tim Hammerquist
--
As a computer, I find your faith in technology amusing.
More information about the Python-list
mailing list