subprocess module

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

    subprocess module

    from subprocess import call
    call(['ls', '-l'])

    How do I get the result (not the exit status of the command) of "ls -
    l" into a variable?
  • Peter Otten

    #2
    Re: subprocess module

    John Mechaniks wrote:
    from subprocess import call
    call(['ls', '-l'])
    >
    How do I get the result (not the exit status of the command) of "ls -
    l" into a variable?
    output = subprocess.Pope n(["ls", "-l"], stdout=subproce ss.PIPE).stdout .read()

    Peter

    Comment

    • sukkopera@gmail.com

      #3
      Re: subprocess module

      On 14 Lug, 10:34, Peter Otten <__pete...@web. dewrote:
      John Mechaniks wrote:
      from subprocess import call
      call(['ls', '-l'])
      >
      How do I get the result (not the exit status of the command) of "ls -
      l" into a variable?
      >
      output = subprocess.Pope n(["ls", "-l"], stdout=subproce ss.PIPE).stdout .read()
      >
      Peter
      Correct, but I would rather use Python's os.listdir() and/or
      os.stat(). Executing a simple ls running a subprocess is overkill.

      Comment

      • John Mechaniks

        #4
        Re: subprocess module

        On Jul 14, 12:34 pm, Peter Otten <__pete...@web. dewrote:
        John Mechaniks wrote:
        from subprocess import call
        call(['ls', '-l'])
        >
        How do I get the result (not the exit status of the command) of "ls -
        l" into a variable?
        >
        output = subprocess.Pope n(["ls", "-l"], stdout=subproce ss.PIPE).stdout .read()
        >
        Peter
        Thanks Peter.

        Just curious

        What difference does the following code makes? What are the advantages
        of the above method over this one?
        output = subprocess.Pope n(['ls', '-l'],
        stdout=subproce ss.PIPE).commun icate()[0]

        Also could someone show an example of using the optional input
        argument for communicate()

        Comment

        • Peter Otten

          #5
          Re: subprocess module

          John Mechaniks wrote:
          On Jul 14, 12:34 pm, Peter Otten <__pete...@web. dewrote:
          >John Mechaniks wrote:
          from subprocess import call
          call(['ls', '-l'])
          >>
          How do I get the result (not the exit status of the command) of "ls -
          l" into a variable?
          >>
          >output = subprocess.Pope n(["ls", "-l"],
          >stdout=subproc ess.PIPE).stdou t.read()
          What difference does the following code makes? What are the advantages
          of the above method over this one?
          output = subprocess.Pope n(['ls', '-l'],
          stdout=subproce ss.PIPE).commun icate()[0]
          Hm, I chose it because it looks cleaner. Looking into the source
          Popen.communica te() seems to do the following:

          output = p._fo_read_no_i ntr(p.stdout)
          p.wait()

          So there are two differences in this case

          - communicate() waits for the subprocess to terminate.
          - stdout.read() is retried if an EINTR occurs (Not sure when this would
          happen).
          Also could someone show an example of using the optional input
          argument for communicate()


          I didn't read it myself, but Doug Hellmann's articles are usually quite
          good.

          Peter

          Comment

          • John Mechaniks

            #6
            Re: subprocess module

            On Jul 14, 7:44 pm, Peter Otten <__pete...@web. dewrote:
            John Mechaniks wrote:
            On Jul 14, 12:34 pm, Peter Otten <__pete...@web. dewrote:
            John Mechaniks wrote:
            from subprocess import call
            call(['ls', '-l'])
            >
            How do I get the result (not the exit status of the command) of "ls -
            l" into a variable?
            >
            output = subprocess.Pope n(["ls", "-l"],
            stdout=subproce ss.PIPE).stdout .read()
            What difference does the following code makes? What are the advantages
            of the above method over this one?
            output = subprocess.Pope n(['ls', '-l'],
            stdout=subproce ss.PIPE).commun icate()[0]
            >
            Hm, I chose it because it looks cleaner. Looking into the source
            Popen.communica te() seems to do the following:
            >
            output = p._fo_read_no_i ntr(p.stdout)  
            p.wait()            
            >
            So there are two differences in this case
            >
            - communicate() waits for the subprocess to terminate.
            - stdout.read() is retried if an EINTR occurs (Not sure when this would
            happen).
            >
            Also could someone show an example of using the optional input
            argument for communicate()
            >

            >
            I didn't read it myself, but Doug Hellmann's articles are usually quite
            good.
            >
            Peter
            Thanks Peter

            Comment

            • Sebastian \lunar\ Wiesner

              #7
              Re: subprocess module

              Peter Otten <__peter__@web. de>:
              - communicate() waits for the subprocess to terminate.
              - stdout.read() is retried if an EINTR occurs (Not sure when this would
              happen).
              EINTR happens, if the "read" syscall is interrupted by a signal handler.
              For instance, if a daemon handles SIGUSR to re-read its configuration, and
              the user issues SIGUSR in exactly that very moment, the daemon wants to
              read from a pipe, the read attempt would fail with EINTR. Shouldn't happen
              that often ...

              --
              Freedom is always the freedom of dissenters.
              (Rosa Luxemburg)

              Comment

              Working...