subprocess.popen function with quotes

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

    subprocess.popen function with quotes

    Hi,
    i'm trying to call subprocess.pope n on the 'rename' function in
    linux. When I run the command from the shell, like so:

    rename -vn 's/\.htm$/\.html/' *.htm

    it works fine... however when I try to do it in python like so:
    p = subprocess.Pope n(["rename","-vn","'s/\.htm$/
    \.html/'","*.htm"],stdout=subproc ess.PIPE,stderr =subprocess.PIP E)

    print p.communicate()[0]

    nothing gets printed out (even for p.communicate()[1])

    I think the problem is the quoted string the rename command wants -
    when I put it in triple quotes like """s/\.htm$/\.html/""" I get some
    output, but not the correct output. I've also tried escaping the
    single quotes with \' and putting it in regular double quotes but that
    didn't work either.

    i'd appreciate any help

  • skunkwerk

    #2
    Re: subprocess.pope n function with quotes

    also, i've tried the Shell=True parameter for Popen, but that didn't
    seem to make a difference

    On Mar 25, 8:31 pm, skunkwerk <skunkw...@gmai l.comwrote:
    Hi,
    i'm trying to call subprocess.pope n on the 'rename' function in
    linux. When I run the command from the shell, like so:
    >
    rename -vn 's/\.htm$/\.html/' *.htm
    >
    it works fine... however when I try to do it in python like so:
    p = subprocess.Pope n(["rename","-vn","'s/\.htm$/
    \.html/'","*.htm"],stdout=subproc ess.PIPE,stderr =subprocess.PIP E)
    >
    print p.communicate()[0]
    >
    nothing gets printed out (even for p.communicate()[1])
    >
    I think the problem is the quoted string the rename command wants -
    when I put it in triple quotes like """s/\.htm$/\.html/""" I get some
    output, but not the correct output. I've also tried escaping the
    single quotes with \' and putting it in regular double quotes but that
    didn't work either.
    >
    i'd appreciate any help

    Comment

    • Gabriel Genellina

      #3
      Re: subprocess.pope n function with quotes

      En Wed, 26 Mar 2008 00:39:05 -0300, skunkwerk <skunkwerk@gmai l.com>
      escribió:
      > i'm trying to call subprocess.pope n on the 'rename' function in
      >linux. When I run the command from the shell, like so:
      >>
      >rename -vn 's/\.htm$/\.html/' *.htm
      >>
      >it works fine... however when I try to do it in python like so:
      >p = subprocess.Pope n(["rename","-vn","'s/\.htm$/
      >\.html/'","*.htm"],stdout=subproc ess.PIPE,stderr =subprocess.PIP E)
      >>
      >print p.communicate()[0]
      >>
      >nothing gets printed out (even for p.communicate()[1])
      I'd try with:

      p = subprocess.Pope n(["rename", "-vn", r"'s/\.htm$/\.html/'", "*.htm"],
      stdout=subproce ss.PIPE, stderr=subproce ss.PIPE,
      shell=True)

      (note that I added shell=True and I'm using a raw string to specify the
      reg.expr.)

      --
      Gabriel Genellina

      Comment

      • skunkwerk

        #4
        Re: subprocess.pope n function with quotes

        On Mar 25, 9:25 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
        wrote:
        En Wed, 26 Mar 2008 00:39:05 -0300, skunkwerk <skunkw...@gmai l.com 
        escribió:
        >
           i'm trying to call subprocess.pope n on the 'rename' function in
        linux.  When I run the command from the shell, like so:
        >
        rename -vn 's/\.htm$/\.html/' *.htm
        >
        it works fine... however when I try to do it in python like so:
        p = subprocess.Pope n(["rename","-vn","'s/\.htm$/
        \.html/'","*.htm"],stdout=subproc ess.PIPE,stderr =subprocess.PIP E)
        >
        print p.communicate()[0]
        >
        nothing gets printed out (even for p.communicate()[1])
        >
        I'd try with:
        >
        p = subprocess.Pope n(["rename", "-vn", r"'s/\.htm$/\.html/'", "*.htm"],
               stdout=subproce ss.PIPE, stderr=subproce ss.PIPE,
               shell=True)
        >
        (note that I added shell=True and I'm using a raw string to specify the  
        reg.expr.)
        >
        --
        Gabriel Genellina
        Thanks Gabriel,
        I tried the new command and one with the raw string and single
        quotes, but it is still giving me the same results (no output). any
        other suggestions?

        cheers

        Comment

        • Gabriel Genellina

          #5
          Re: subprocess.pope n function with quotes

          En Wed, 26 Mar 2008 02:15:28 -0300, skunkwerk <skunkwerk@gmai l.com>
          escribió:
          On Mar 25, 9:25 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
          wrote:
          >En Wed, 26 Mar 2008 00:39:05 -0300, skunkwerk <skunkw...@gmai l.com 
          >escribió:
          >>
          >   i'm trying to call subprocess.pope n on the 'rename' function in
          >linux.  When I run the command from the shell, like so:
          >>
          >rename -vn 's/\.htm$/\.html/' *.htm
          >>
          >it works fine... however when I try to do it in python like so:
          >p = subprocess.Pope n(["rename","-vn","'s/\.htm$/
          >\.html/'","*.htm"],stdout=subproc ess.PIPE,stderr =subprocess.PIP E)
          >>
          >print p.communicate()[0]
          >>
          >nothing gets printed out (even for p.communicate()[1])
          >>
          >I'd try with:
          >>
          >p = subprocess.Pope n(["rename", "-vn", r"'s/\.htm$/\.html/'", "*.htm"],
          >       stdout=subproce ss.PIPE, stderr=subproce ss.PIPE,
          >       shell=True)
          >>
          >(note that I added shell=True and I'm using a raw string to specify the
          >reg.expr.)
          >
          Thanks Gabriel,
          I tried the new command and one with the raw string and single
          quotes, but it is still giving me the same results (no output). any
          other suggestions?
          My next try would be without the single quotes...

          --
          Gabriel Genellina

          Comment

          • skunkwerk

            #6
            Re: subprocess.pope n function with quotes

            On Mar 25, 11:04 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
            wrote:
            En Wed, 26 Mar 2008 02:15:28 -0300, skunkwerk <skunkw...@gmai l.com 
            escribió:
            >
            >
            >
            On Mar 25, 9:25 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
            wrote:
            En Wed, 26 Mar 2008 00:39:05 -0300, skunkwerk <skunkw...@gmai l.com 
            escribió:
            >
               i'm trying to call subprocess.pope n on the 'rename' function in
            linux.  When I run the command from the shell, like so:
            >
            rename -vn 's/\.htm$/\.html/' *.htm
            >
            it works fine... however when I try to do it in python like so:
            p = subprocess.Pope n(["rename","-vn","'s/\.htm$/
            \.html/'","*.htm"],stdout=subproc ess.PIPE,stderr =subprocess.PIP E)
            >
            print p.communicate()[0]
            >
            nothing gets printed out (even for p.communicate()[1])
            >
            I'd try with:
            >
            p = subprocess.Pope n(["rename", "-vn", r"'s/\.htm$/\.html/'", "*.htm"],
                   stdout=subproce ss.PIPE, stderr=subproce ss.PIPE,
                   shell=True)
            >
            (note that I added shell=True and I'm using a raw string to specify the  
            reg.expr.)
            >
            Thanks Gabriel,
               I tried the new command and one with the raw string and single
            quotes, but it is still giving me the same results (no output).  any
            other suggestions?
            >
            My next try would be without the single quotes...
            >
            --
            Gabriel Genellina
            thanks for the input guys,
            I've tried the suggestions but can't get it to work. I have a file
            named test.htm in my directory, and when I run the following command:

            rename -vn 's/(.*)\.htm$/model.html/' *.htm

            from the shell in that directory I get the following output:
            test.htm renamed as model.html

            now my python script is called test.py, is located in the same
            directory, and is called from the shell with 'python test.py'
            the contents of test.py:
            import subprocess

            p = subprocess.Pope n(['rename','-vn','s/(.*)\.htm$/
            model.html/','*.htm'],stdout=subproc ess.PIPE,stderr =subprocess.PIP E)
            print p.communicate()[0]

            i change to print p.communicate()[1] in case the output is blank the
            first time

            this is the output:
            *.htm renamed as model.html

            when I add shell=True to the subprocess command, I get the following
            output:
            Usage: rename [-v] [-n] [-f] perlexpr [filenames]

            am i doing something wrong?

            Comment

            • skunkwerk

              #7
              Re: subprocess.pope n function with quotes

              On Mar 26, 6:44 am, skunkwerk <skunkw...@gmai l.comwrote:
              On Mar 25, 11:04 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
              wrote:
              >
              >
              >
              En Wed, 26 Mar 2008 02:15:28 -0300, skunkwerk <skunkw...@gmai l.com 
              escribió:
              >
              On Mar 25, 9:25 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
              wrote:
              >En Wed, 26 Mar 2008 00:39:05 -0300, skunkwerk <skunkw...@gmai l.com 
              >escribió:
              >
              >   i'm trying to call subprocess.pope n on the 'rename' function in
              >linux.  When I run the command from the shell, like so:
              >
              >rename -vn 's/\.htm$/\.html/' *.htm
              >
              >it works fine... however when I try to do it in python like so:
              >p = subprocess.Pope n(["rename","-vn","'s/\.htm$/
              >\.html/'","*.htm"],stdout=subproc ess.PIPE,stderr =subprocess.PIP E)
              >
              >print p.communicate()[0]
              >
              >nothing gets printed out (even for p.communicate()[1])
              >
              >I'd try with:
              >
              >p = subprocess.Pope n(["rename", "-vn", r"'s/\.htm$/\.html/'", "*.htm"],
              >       stdout=subproce ss.PIPE, stderr=subproce ss.PIPE,
              >       shell=True)
              >
              >(note that I added shell=True and I'm using a raw string to specifythe  
              >reg.expr.)
              >
              Thanks Gabriel,
                 I tried the new command and one with the raw string and single
              quotes, but it is still giving me the same results (no output).  any
              other suggestions?
              >
              My next try would be without the single quotes...
              >
              --
              Gabriel Genellina
              >
              thanks for the input guys,
                I've tried the suggestions but can't get it to work.  I have a file
              named test.htm in my directory, and when I run the following command:
              >
              rename -vn 's/(.*)\.htm$/model.html/' *.htm
              >
              from the shell in that directory I get the following output:
              test.htm renamed as model.html
              >
              now my python script is called test.py, is located in the same
              directory, and is called from the shell with 'python test.py'
              the contents of test.py:
              import subprocess
              >
              p = subprocess.Pope n(['rename','-vn','s/(.*)\.htm$/
              model.html/','*.htm'],stdout=subproc ess.PIPE,stderr =subprocess.PIP E)
              print p.communicate()[0]
              >
              i change to print p.communicate()[1] in case the output is blank the
              first time
              >
              this is the output:
              *.htm renamed as model.html
              >
              when I add shell=True to the subprocess command, I get the following
              output:
              Usage: rename [-v] [-n] [-f] perlexpr [filenames]
              >
              am i doing something wrong?
              in addition, when I use Popen without any quotes, or without quotes
              for the regular expression, I get an exception.

              I'm running ubuntu linux 7.10 with python 2.5.1

              thanks

              Comment

              • skunkwerk

                #8
                Re: subprocess.pope n function with quotes

                On Mar 26, 10:33 pm, skunkwerk <skunkw...@gmai l.comwrote:
                On Mar 26, 8:05 am, Jeffrey Froman <jeff...@fro.ma nwrote:
                >
                >
                >
                skunkwerkwrote:
                p = subprocess.Pope n(['rename','-vn','s/(.*)\.htm$/
                model.html/','*.htm'],stdout=subproc ess.PIPE,stderr =subprocess.PIP E)
                print p.communicate()[0]
                >
                i change to print p.communicate()[1] in case the output is blank the
                first time
                >
                this is the output:
                *.htm renamed as model.html
                >
                Without shell=True, your glob characters will not be expanded. Hence, the
                command looks for a file actually named "*.htm"
                >
                when I add shell=True to the subprocess command, I get the following
                output:
                Usage: rename [-v] [-n] [-f] perlexpr [filenames]
                >
                Here the use of the shell may be confounding the arguments passed. Your
                command will probably work better if you avoid using shell=True. However,
                you will need to perform your own globbing:
                >
                # Untested (no perl-rename here):
                >
                command = ['rename','-vn', 's/(.*)\.htm$/model.html/']
                files = glob.glob('*.ht m')
                command.extend( files)
                p = subprocess.Pope n(
                    command,
                    stdout=subproce ss.PIPE,
                    stderr=subproce ss.PIPE,
                    )
                >
                Jeffrey
                >
                thanks Jeffrey, that worked like a charm!
                I'm trying to detect when the subprocess has terminated using the
                wait() function - but when there is an error with the call to rename
                (ie the file doesn't exist) rename (when run from the command line
                just terminates and displays the error). In the code above, though,
                my call to p.wait() just hangs when rename should throw an error...
                I've tried adding shell=True but that stops the rename from working.
                any ideas?

                thanks

                Comment

                Working...