Simple question for all of you python gurus

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sean Berry

    Simple question for all of you python gurus

    I am writing a little script to grep through a really long list of files and
    look for a certain phrase.

    I wanted to do something like this.

    li = os.popen("/usr/bin/grep -i %s /home/%s/.tmda/pending" %(phrase,userid )
    lis = li.readlines()
    blah blah blah...

    but even if I run this from the command line I get an error saying the
    argument list is too long.

    However, if I cd to /home/userid/.tmda/pending and then run the grep from
    within the directory... it works great.

    so I tried to do something like this
    os.system("cd /home/%s/.tmda/pending" %userid)
    li = os.popen("/usr/bin/grep -i %s" %phase)
    lis = lis.readlines()

    But since cd is a shell command, it does not seem to work either.

    How can I do this?
    Thanks in advance for answering this seemingly easy question.



  • Diez B. Roggisch

    #2
    Re: Simple question for all of you python gurus

    > How can I do this?[color=blue]
    > Thanks in advance for answering this seemingly easy question.[/color]

    You're code might work if you use os.chdir to change the current working
    directory.

    Another that works on the command-line (and thus for your script) is to use
    xargs with -l<num>, e.g.:

    find / | xargs -l 10 grep -i foo

    xargs then will invoke grep in batches of ten.



    --
    Regards,

    Diez B. Roggisch

    Comment

    • Cameron Laird

      #3
      Re: Simple question for all of you python gurus

      In article <NYu%b.12366$qL 1.145@fed1read0 2>,
      Sean Berry <sean_berry@cox .net> wrote:

      Comment

      • Sean Berry

        #4
        Re: Simple question for all of you python gurus

        Thanks to you both.

        os.chdir ... I knew it was something simple. Sorry for the wasted
        bandwidth.

        Cameron Laird <claird@lairds. com> wrote in message
        news:103svepnqo vuk5e@corp.supe rnews.com...[color=blue]
        > In article <NYu%b.12366$qL 1.145@fed1read0 2>,
        > Sean Berry <sean_berry@cox .net> wrote:
        > .
        > .
        > .[color=green]
        > >so I tried to do something like this
        > >os.system("c d /home/%s/.tmda/pending" %userid)
        > >li = os.popen("/usr/bin/grep -i %s" %phase)
        > >lis = lis.readlines()
        > >
        > >But since cd is a shell command, it does not seem to work either.
        > >
        > >How can I do this?[/color]
        > .
        > .
        > .
        > import os
        >
        > os.chdir("/home/%s/.tmda/pending" % userid)
        > li = os.popen(...
        > --
        >
        > Cameron Laird <claird@phaseit .net>
        > Business: http://www.Phaseit.net[/color]


        Comment

        • Jeremy Sanders

          #5
          Re: Simple question for all of you python gurus

          On Thu, 26 Feb 2004 23:14:33 +0000, Cameron Laird wrote:
          [color=blue]
          > os.chdir("/home/%s/.tmda/pending" % userid)
          > li = os.popen(...[/color]

          Wouldn't something like this also work?

          li = os.popen('/bin/sh -c "cd /home/%s; /usr/bin/grep -i %s blah"')

          This has the advantage that the current directory of the Python program
          doesn't change.

          Jeremy

          Comment

          • Cameron Laird

            #6
            Re: Simple question for all of you python gurus

            In article <pan.2004.02.27 .10.23.57.12660 0@jeremysanders .net>,
            Jeremy Sanders <jeremy+plusnew s@jeremysanders .net> wrote:[color=blue]
            >On Thu, 26 Feb 2004 23:14:33 +0000, Cameron Laird wrote:
            >[color=green]
            >> os.chdir("/home/%s/.tmda/pending" % userid)
            >> li = os.popen(...[/color]
            >
            >Wouldn't something like this also work?
            >
            >li = os.popen('/bin/sh -c "cd /home/%s; /usr/bin/grep -i %s blah"')
            >
            >This has the advantage that the current directory of the Python program
            >doesn't change.[/color]

            Comment

            Working...