Why use backticks?
Mark Day
mday at apple.com
Wed May 7 20:59:41 EDT 2003
In article <vbj19oogjo4gfc at corp.supernews.com>, Paul Watson
<pwatson at redlinec.com> wrote:
> Backticks in Python do not mean the same thing as they do in Perl. Your
> Perl friend would need to do something like:
>
> $ python
> Python 2.1 (#1, Dec 6 2002, 19:28:59) [C] on aix4
> Type "copyright", "credits" or "license" for more information.
> >>> import os
> >>> f = os.popen('ls -al')
> >>> x = f.readlines()
> >>> for aline in x:
> ... print aline.strip()
> ...
No need to print the output a line at a time. To slurp the entire
output and print it as one string, you could do:
>>> import os,sys
>>> f = os.popen('ls -al')
>>> x = f.read()
>>> sys.stdout.write(x)
Of course, you could just "print x.strip()" as the last line, but that
strips all leading and trailing whitespace, which might not be what you
want in general.
-Mark
More information about the Python-list
mailing list