Run a subprocess in python and both show the output in “real time” and save it to a variable2019 Community Moderator ElectionActual meaning of 'shell=True' in subprocessUse of *args and **kwargsPipe subprocess standard output to a variableRunning shell command and capturing the outputFastest way to check if a value exist in a listIssues intercepting subprocess output in real timeWhy is “1000000000000000 in range(1000000000000001)” so fast in Python 3?Environment variables not saved through subsequent subprocess calls (Python)Python subprocess to get real output and load output to variablesubprocess popen real time output with timeout

How is the Swiss post e-voting system supposed to work, and how was it wrong?

Humans have energy, but not water. What happens?

Do I need to leave some extra space available on the disk which my database log files reside, for log backup operations to successfully occur?

Force user to remove USB token

Excess Zinc in garden soil

When is a batch class instantiated when you schedule it?

It's a yearly task, alright

Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?

If Invisibility ends because the original caster casts a non-concentration spell, does Invisibility also end on other targets of the original casting?

If the Captain's screens are out, does he switch seats with the co-pilot?

What is the likely impact on flights of grounding an entire aircraft series?

Is it ok to include an epilogue dedicated to colleagues who passed away in the end of the manuscript?

Is having access to past exams cheating and, if yes, could it be proven just by a good grade?

Want to switch to tankless, but can I use my existing wiring?

How could a female member of a species produce eggs unto death?

What wound would be of little consequence to a biped but terrible for a quadruped?

The three point beverage

Does the Bracer of Flying Daggers benefit from the Dueling Fighting style?

Word for a person who has no opinion about whether god exists

Making a sword in the stone, in a medieval world without magic

Good for you! in Russian

How to make readers know that my work has used a hidden constraint?

Silly Sally's Movie

Provisioning profile doesn't include the application-identifier and keychain-access-groups entitlements



Run a subprocess in python and both show the output in “real time” and save it to a variable



2019 Community Moderator ElectionActual meaning of 'shell=True' in subprocessUse of *args and **kwargsPipe subprocess standard output to a variableRunning shell command and capturing the outputFastest way to check if a value exist in a listIssues intercepting subprocess output in real timeWhy is “1000000000000000 in range(1000000000000001)” so fast in Python 3?Environment variables not saved through subsequent subprocess calls (Python)Python subprocess to get real output and load output to variablesubprocess popen real time output with timeout










1















I would like to be able to run a subprocess from python code and both see the output in real time and once the process is finished have the output in a variable



Right now I do one of either two things



1) Run subprocess using subprocess.call in that case I get the output in real time but I don't have at the end the output in a variable (I want to parse it and extract values from it)



2) Run subprocess using subprocess.check_output in that case I have the output in a variable but if I want to see it then I have to print it "manually"



Is there a way to get both things "together" ?



Hope it is clear, I can add my code if you need



Thanks !!!



EDIT:



This is my current code



enter image description here



I added a timeout optional parameter (Default value is 1200 and also deal with shell (For some reason same commands that work in Linux do not work in Windows if I don't have the shell=True) the "mode" parameter is the one that I use to differentiate the cases where I want the output in "real time" and I don't have to parse it and the other cases



I was wondering if there is a cleaner and better way to achieve same results










share|improve this question
























  • do you have any code that's not working or have you tried anything yet?

    – systrigger
    Mar 7 at 11:11











  • My code is working but I want to know if there is a better way to do this, right now I do something like this

    – David Fliguer
    Mar 7 at 12:27















1















I would like to be able to run a subprocess from python code and both see the output in real time and once the process is finished have the output in a variable



Right now I do one of either two things



1) Run subprocess using subprocess.call in that case I get the output in real time but I don't have at the end the output in a variable (I want to parse it and extract values from it)



2) Run subprocess using subprocess.check_output in that case I have the output in a variable but if I want to see it then I have to print it "manually"



Is there a way to get both things "together" ?



Hope it is clear, I can add my code if you need



Thanks !!!



EDIT:



This is my current code



enter image description here



I added a timeout optional parameter (Default value is 1200 and also deal with shell (For some reason same commands that work in Linux do not work in Windows if I don't have the shell=True) the "mode" parameter is the one that I use to differentiate the cases where I want the output in "real time" and I don't have to parse it and the other cases



I was wondering if there is a cleaner and better way to achieve same results










share|improve this question
























  • do you have any code that's not working or have you tried anything yet?

    – systrigger
    Mar 7 at 11:11











  • My code is working but I want to know if there is a better way to do this, right now I do something like this

    – David Fliguer
    Mar 7 at 12:27













1












1








1








I would like to be able to run a subprocess from python code and both see the output in real time and once the process is finished have the output in a variable



Right now I do one of either two things



1) Run subprocess using subprocess.call in that case I get the output in real time but I don't have at the end the output in a variable (I want to parse it and extract values from it)



2) Run subprocess using subprocess.check_output in that case I have the output in a variable but if I want to see it then I have to print it "manually"



Is there a way to get both things "together" ?



Hope it is clear, I can add my code if you need



Thanks !!!



EDIT:



This is my current code



enter image description here



I added a timeout optional parameter (Default value is 1200 and also deal with shell (For some reason same commands that work in Linux do not work in Windows if I don't have the shell=True) the "mode" parameter is the one that I use to differentiate the cases where I want the output in "real time" and I don't have to parse it and the other cases



I was wondering if there is a cleaner and better way to achieve same results










share|improve this question
















I would like to be able to run a subprocess from python code and both see the output in real time and once the process is finished have the output in a variable



Right now I do one of either two things



1) Run subprocess using subprocess.call in that case I get the output in real time but I don't have at the end the output in a variable (I want to parse it and extract values from it)



2) Run subprocess using subprocess.check_output in that case I have the output in a variable but if I want to see it then I have to print it "manually"



Is there a way to get both things "together" ?



Hope it is clear, I can add my code if you need



Thanks !!!



EDIT:



This is my current code



enter image description here



I added a timeout optional parameter (Default value is 1200 and also deal with shell (For some reason same commands that work in Linux do not work in Windows if I don't have the shell=True) the "mode" parameter is the one that I use to differentiate the cases where I want the output in "real time" and I don't have to parse it and the other cases



I was wondering if there is a cleaner and better way to achieve same results







python subprocess






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 12:34







David Fliguer

















asked Mar 7 at 9:39









David FliguerDavid Fliguer

506




506












  • do you have any code that's not working or have you tried anything yet?

    – systrigger
    Mar 7 at 11:11











  • My code is working but I want to know if there is a better way to do this, right now I do something like this

    – David Fliguer
    Mar 7 at 12:27

















  • do you have any code that's not working or have you tried anything yet?

    – systrigger
    Mar 7 at 11:11











  • My code is working but I want to know if there is a better way to do this, right now I do something like this

    – David Fliguer
    Mar 7 at 12:27
















do you have any code that's not working or have you tried anything yet?

– systrigger
Mar 7 at 11:11





do you have any code that's not working or have you tried anything yet?

– systrigger
Mar 7 at 11:11













My code is working but I want to know if there is a better way to do this, right now I do something like this

– David Fliguer
Mar 7 at 12:27





My code is working but I want to know if there is a better way to do this, right now I do something like this

– David Fliguer
Mar 7 at 12:27












2 Answers
2






active

oldest

votes


















0














Assuming you are trying to run some command your_command You can use the following:



some_proc = subprocess.Popen(['your_command'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)


The stdout=subprocess.PIPE does stdout the result on success. Afterwards, you can access the output as follows:



store_in_var = some_proc.stdout


Now you can parse your store_in_var






share|improve this answer






























    0














    import subprocess
    from subprocess import PIPE

    comd = input('command here : ')
    comds = comd.split(' ')
    f = subprocess.run(comds, shell= True,stdout=PIPE, stderr=PIPE)
    result = f.stdout.decode()
    errors = f.stderr.decode()





    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55040507%2frun-a-subprocess-in-python-and-both-show-the-output-in-real-time-and-save-it-t%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Assuming you are trying to run some command your_command You can use the following:



      some_proc = subprocess.Popen(['your_command'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)


      The stdout=subprocess.PIPE does stdout the result on success. Afterwards, you can access the output as follows:



      store_in_var = some_proc.stdout


      Now you can parse your store_in_var






      share|improve this answer



























        0














        Assuming you are trying to run some command your_command You can use the following:



        some_proc = subprocess.Popen(['your_command'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)


        The stdout=subprocess.PIPE does stdout the result on success. Afterwards, you can access the output as follows:



        store_in_var = some_proc.stdout


        Now you can parse your store_in_var






        share|improve this answer

























          0












          0








          0







          Assuming you are trying to run some command your_command You can use the following:



          some_proc = subprocess.Popen(['your_command'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)


          The stdout=subprocess.PIPE does stdout the result on success. Afterwards, you can access the output as follows:



          store_in_var = some_proc.stdout


          Now you can parse your store_in_var






          share|improve this answer













          Assuming you are trying to run some command your_command You can use the following:



          some_proc = subprocess.Popen(['your_command'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)


          The stdout=subprocess.PIPE does stdout the result on success. Afterwards, you can access the output as follows:



          store_in_var = some_proc.stdout


          Now you can parse your store_in_var







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 7 at 11:17









          systriggersystrigger

          879




          879























              0














              import subprocess
              from subprocess import PIPE

              comd = input('command here : ')
              comds = comd.split(' ')
              f = subprocess.run(comds, shell= True,stdout=PIPE, stderr=PIPE)
              result = f.stdout.decode()
              errors = f.stderr.decode()





              share|improve this answer



























                0














                import subprocess
                from subprocess import PIPE

                comd = input('command here : ')
                comds = comd.split(' ')
                f = subprocess.run(comds, shell= True,stdout=PIPE, stderr=PIPE)
                result = f.stdout.decode()
                errors = f.stderr.decode()





                share|improve this answer

























                  0












                  0








                  0







                  import subprocess
                  from subprocess import PIPE

                  comd = input('command here : ')
                  comds = comd.split(' ')
                  f = subprocess.run(comds, shell= True,stdout=PIPE, stderr=PIPE)
                  result = f.stdout.decode()
                  errors = f.stderr.decode()





                  share|improve this answer













                  import subprocess
                  from subprocess import PIPE

                  comd = input('command here : ')
                  comds = comd.split(' ')
                  f = subprocess.run(comds, shell= True,stdout=PIPE, stderr=PIPE)
                  result = f.stdout.decode()
                  errors = f.stderr.decode()






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 7 at 11:21









                  AkhileshAkhilesh

                  12110




                  12110



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55040507%2frun-a-subprocess-in-python-and-both-show-the-output-in-real-time-and-save-it-t%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

                      Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                      Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite