Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Python - Retrieving output from os.system()

  1. #1
    Join Date
    Dec 2007
    Location
    Behind you!!
    Beans
    978
    Distro
    Ubuntu 10.04 Lucid Lynx

    [SOLVED]Python - Retrieving output from os.system()

    Hi guys,

    I am having a bit of trouble retrieving information output from os.system() calls. To be specific, I am trying to do the following:
    Code:
    cwd = os.system("pwd")
    I was hoping to get the current working directory string that the above command prints out, stored in cwd, instead I get the exit status from the 'pwd' command.

    Is there any way to retrieve the data I need, through os.system() or any other type of system call.

    Kind regards,
    Bodsda
    Last edited by Bodsda; August 31st, 2009 at 09:31 PM.
    computer-howto
    Linux is not windows
    Fluxbox & Flux menu how to
    Programming is an art. Learn it, Live it, Love it!


  2. #2
    credobyte is offline Iced Blended Vanilla Crème Ubuntu
    Join Date
    Jun 2009
    Beans
    1,559
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Python - Retrieving output from os.system()

    I'm far away from "I know Python", but .. answer could be found @ http://mail.python.org/pipermail/pyt...er/343901.html

  3. #3
    Join Date
    Dec 2007
    Location
    Behind you!!
    Beans
    978
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python - Retrieving output from os.system()

    Quote Originally Posted by credobyte View Post
    I'm far away from "I know Python", but .. answer could be found @ http://mail.python.org/pipermail/pyt...er/343901.html
    Yeah, I tried using the subprocess module, specifically:
    Code:
    cwd = subprocess.call(["pwd"])
    But I just got the exit status again.

    Thanks,
    Kind regards,
    Bodsda
    computer-howto
    Linux is not windows
    Fluxbox & Flux menu how to
    Programming is an art. Learn it, Live it, Love it!


  4. #4
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Python - Retrieving output from os.system()

    Hi Bodsda, how about this:
    PHP Code:
    #!/usr/bin/env python
    from subprocess import Popen,PIPE,STDOUT,call

    proc
    =Popen('pwd'shell=Truestdout=PIPE, )
    output=proc.communicate()[0]
    print 
    output 
    or, in this case,
    PHP Code:

    import os
    print(os.getcwd()) 
    PS. The subprocess code I learned from Doug Hellmann's wonderful Python Module of the Week blog: http://blog.doughellmann.com/2007/07...ubprocess.html

  5. #5
    Join Date
    Dec 2007
    Location
    Behind you!!
    Beans
    978
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python - Retrieving output from os.system()

    Hmm,
    Code:
    ouput = os.popen("pwd").readlines()
    seems to get me a list of the output. Thanks for everyones help.

    Kind regards,
    Bodsda
    computer-howto
    Linux is not windows
    Fluxbox & Flux menu how to
    Programming is an art. Learn it, Live it, Love it!


  6. #6
    Join Date
    Dec 2007
    Location
    Behind you!!
    Beans
    978
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python - Retrieving output from os.system()

    Quote Originally Posted by unutbu View Post
    Hi Bodsda, how about this:
    PHP Code:
    #!/usr/bin/env python
    from subprocess import Popen,PIPE,STDOUT,call

    proc
    =Popen('pwd'shell=Truestdout=PIPE, )
    output=proc.communicate()[0]
    print 
    output 
    or, in this case,
    PHP Code:

    import os
    print(os.getcwd()) 
    PS. The subprocess code I learned from Doug Hellmann's wonderful Python Module of the Week blog: http://blog.doughellmann.com/2007/07...ubprocess.html
    Hi unutbu,

    Could you explain your first code block please. I'm new to the popen attribute

    Thanks,
    Kind regards,
    Bodsda
    computer-howto
    Linux is not windows
    Fluxbox & Flux menu how to
    Programming is an art. Learn it, Live it, Love it!


  7. #7
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Python - Retrieving output from os.system()

    Explanation? Uh-oh...
    Here is where I expose my lack of understanding:
    Code:
    proc=Popen('pwd', shell=True, stdout=PIPE)
    This makes proc an instance of the Popen class.
    This line, as I understand it, sets up a subshell which runs the 'pwd' command.
    The stdout file handle is directed to PIPE. If you don't do this, then the subshell prints its output to the terminal. By directing it to the PIPE, you can then read what has been sent to the PIPE via the command
    Code:
    proc.communicate()
    proc.communicate() returns a 2-tuple: (stdout, stderr)

    So
    Code:
    output=proc.communicate()[0]
    picks of the stdout, and ignores stderr.

    Hope this helps. I'm sure Doug Hellmann explains it better.

    PS. The official documentation on the subprocess module can be found here: http://docs.python.org/library/subpr...ule-subprocess
    Last edited by unutbu; August 31st, 2009 at 07:33 PM.

  8. #8
    Join Date
    Dec 2007
    Location
    Behind you!!
    Beans
    978
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python - Retrieving output from os.system()

    Quote Originally Posted by unutbu View Post
    Explanation? Uh-oh...
    Here is where I expose my lack of understanding:
    Code:
    proc=Popen('pwd', shell=True, stdout=PIPE)
    This makes proc an instance of the Popen class.
    This line, as I understand it, sets up a subshell which runs the 'pwd' command.
    The stdout file handle is directed to PIPE. If you don't do this, then the subshell prints its output to the terminal. By directing it to the PIPE, you can then read what has been sent to the PIPE via the command
    Code:
    proc.communicate()
    proc.communicate() returns a 2-tuple: (stdout, stderr)

    So
    Code:
    output=proc.communicate()[0]
    picks of the stdout, and ignores stderr.

    Hope this helps. I'm sure Doug Hellmann explains it better.

    PS. The official documentation on the subprocess module can be found here: http://docs.python.org/library/subpr...ule-subprocess
    Brilliant, thanks again unutbu,

    Kind regards,
    Bodsda
    computer-howto
    Linux is not windows
    Fluxbox & Flux menu how to
    Programming is an art. Learn it, Live it, Love it!


  9. #9
    Join Date
    Apr 2005
    Location
    Germany
    Beans
    215

    Re: Python - Retrieving output from os.system()

    PHP Code:
    import commands
    print commands.getoutput("pwd"
    Ubuntu is an ancient African word meaning ‘I can’t configure Debian’

  10. #10
    Join Date
    Dec 2007
    Location
    .
    Beans
    Hidden!
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Python - Retrieving output from os.system()

    Code:
    import os
    print os.getcwd()

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •