Why use backticks?

Paul Watson pwatson at redlinec.com
Wed May 7 23:36:45 EDT 2003


Do you think there would be any interest in having backticks mean the same
thing as in Perl?

x = `ls -al`

would turn into x = os.popen('ls -al') with an rstrip('\n') on each line?

Would this be opening too big of a can?


"Mark Day" <mday at apple.com> wrote in message
news:070520031759417955%mday at apple.com...
> 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