Store filtered output of cmd command in a variable

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite












I am trying to store the output of a cmd command as a variable in python.
To achieve this i am using os.system() but os.system() just runs the process,it doesn't capture the output.



import os


PlatformName = os.system("adb shell getprop | grep -e 'bt.name'")
DeviceName = os.system("adb shell getprop | grep -e '.product.brand'")
DeviceID = os.system("adb shell getprop | grep -e 'serialno'")
Version = os.system("adb shell getprop | grep -e 'version.release'")

print(PlatformName)
print(DeviceName)
print(DeviceID)
print(Version)


Then i tried to use the subprocess module.



import subprocess
import os


PlatformName = subprocess.check_output(["adb shell getprop | grep -e 'bt.name'"])
DeviceName = subprocess.check_output(["adb shell getprop | grep -e '.product.brand'"])
DeviceID = subprocess.check_output(["adb shell getprop | grep -e 'serialno'"])
Version = subprocess.check_output(["adb shell getprop | grep -e 'version.release'"])

print(PlatformName)
print(DeviceName)
print(DeviceID)
print(Version)


I am getting the following error




FileNotFoundError: [WinError 2] The system cannot find the file
specified




How can I store the output of the command as a variable?










share|improve this question























  • I think you should just separate each piece of your batch command with a comma.
    – toti08
    1 hour ago










  • Check this question for more info.
    – toti08
    1 hour ago










  • I think you should "native" grep from python... the issue is missing shell=True
    – Jean-François Fabre
    1 hour ago











  • @Jean-FrançoisFabre I have added shell=True and i got CompletedProcess(args=["adb, shell getprop | grep -e 'bt.name'"], returncode=1) but it still does not store the output
    – Wojtek T
    1 hour ago














up vote
7
down vote

favorite












I am trying to store the output of a cmd command as a variable in python.
To achieve this i am using os.system() but os.system() just runs the process,it doesn't capture the output.



import os


PlatformName = os.system("adb shell getprop | grep -e 'bt.name'")
DeviceName = os.system("adb shell getprop | grep -e '.product.brand'")
DeviceID = os.system("adb shell getprop | grep -e 'serialno'")
Version = os.system("adb shell getprop | grep -e 'version.release'")

print(PlatformName)
print(DeviceName)
print(DeviceID)
print(Version)


Then i tried to use the subprocess module.



import subprocess
import os


PlatformName = subprocess.check_output(["adb shell getprop | grep -e 'bt.name'"])
DeviceName = subprocess.check_output(["adb shell getprop | grep -e '.product.brand'"])
DeviceID = subprocess.check_output(["adb shell getprop | grep -e 'serialno'"])
Version = subprocess.check_output(["adb shell getprop | grep -e 'version.release'"])

print(PlatformName)
print(DeviceName)
print(DeviceID)
print(Version)


I am getting the following error




FileNotFoundError: [WinError 2] The system cannot find the file
specified




How can I store the output of the command as a variable?










share|improve this question























  • I think you should just separate each piece of your batch command with a comma.
    – toti08
    1 hour ago










  • Check this question for more info.
    – toti08
    1 hour ago










  • I think you should "native" grep from python... the issue is missing shell=True
    – Jean-François Fabre
    1 hour ago











  • @Jean-FrançoisFabre I have added shell=True and i got CompletedProcess(args=["adb, shell getprop | grep -e 'bt.name'"], returncode=1) but it still does not store the output
    – Wojtek T
    1 hour ago












up vote
7
down vote

favorite









up vote
7
down vote

favorite











I am trying to store the output of a cmd command as a variable in python.
To achieve this i am using os.system() but os.system() just runs the process,it doesn't capture the output.



import os


PlatformName = os.system("adb shell getprop | grep -e 'bt.name'")
DeviceName = os.system("adb shell getprop | grep -e '.product.brand'")
DeviceID = os.system("adb shell getprop | grep -e 'serialno'")
Version = os.system("adb shell getprop | grep -e 'version.release'")

print(PlatformName)
print(DeviceName)
print(DeviceID)
print(Version)


Then i tried to use the subprocess module.



import subprocess
import os


PlatformName = subprocess.check_output(["adb shell getprop | grep -e 'bt.name'"])
DeviceName = subprocess.check_output(["adb shell getprop | grep -e '.product.brand'"])
DeviceID = subprocess.check_output(["adb shell getprop | grep -e 'serialno'"])
Version = subprocess.check_output(["adb shell getprop | grep -e 'version.release'"])

print(PlatformName)
print(DeviceName)
print(DeviceID)
print(Version)


I am getting the following error




FileNotFoundError: [WinError 2] The system cannot find the file
specified




How can I store the output of the command as a variable?










share|improve this question















I am trying to store the output of a cmd command as a variable in python.
To achieve this i am using os.system() but os.system() just runs the process,it doesn't capture the output.



import os


PlatformName = os.system("adb shell getprop | grep -e 'bt.name'")
DeviceName = os.system("adb shell getprop | grep -e '.product.brand'")
DeviceID = os.system("adb shell getprop | grep -e 'serialno'")
Version = os.system("adb shell getprop | grep -e 'version.release'")

print(PlatformName)
print(DeviceName)
print(DeviceID)
print(Version)


Then i tried to use the subprocess module.



import subprocess
import os


PlatformName = subprocess.check_output(["adb shell getprop | grep -e 'bt.name'"])
DeviceName = subprocess.check_output(["adb shell getprop | grep -e '.product.brand'"])
DeviceID = subprocess.check_output(["adb shell getprop | grep -e 'serialno'"])
Version = subprocess.check_output(["adb shell getprop | grep -e 'version.release'"])

print(PlatformName)
print(DeviceName)
print(DeviceID)
print(Version)


I am getting the following error




FileNotFoundError: [WinError 2] The system cannot find the file
specified




How can I store the output of the command as a variable?







python variables cmd






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 55 mins ago









Jean-François Fabre

91.7k847102




91.7k847102










asked 1 hour ago









Wojtek T

774321




774321











  • I think you should just separate each piece of your batch command with a comma.
    – toti08
    1 hour ago










  • Check this question for more info.
    – toti08
    1 hour ago










  • I think you should "native" grep from python... the issue is missing shell=True
    – Jean-François Fabre
    1 hour ago











  • @Jean-FrançoisFabre I have added shell=True and i got CompletedProcess(args=["adb, shell getprop | grep -e 'bt.name'"], returncode=1) but it still does not store the output
    – Wojtek T
    1 hour ago
















  • I think you should just separate each piece of your batch command with a comma.
    – toti08
    1 hour ago










  • Check this question for more info.
    – toti08
    1 hour ago










  • I think you should "native" grep from python... the issue is missing shell=True
    – Jean-François Fabre
    1 hour ago











  • @Jean-FrançoisFabre I have added shell=True and i got CompletedProcess(args=["adb, shell getprop | grep -e 'bt.name'"], returncode=1) but it still does not store the output
    – Wojtek T
    1 hour ago















I think you should just separate each piece of your batch command with a comma.
– toti08
1 hour ago




I think you should just separate each piece of your batch command with a comma.
– toti08
1 hour ago












Check this question for more info.
– toti08
1 hour ago




Check this question for more info.
– toti08
1 hour ago












I think you should "native" grep from python... the issue is missing shell=True
– Jean-François Fabre
1 hour ago





I think you should "native" grep from python... the issue is missing shell=True
– Jean-François Fabre
1 hour ago













@Jean-FrançoisFabre I have added shell=True and i got CompletedProcess(args=["adb, shell getprop | grep -e 'bt.name'"], returncode=1) but it still does not store the output
– Wojtek T
1 hour ago




@Jean-FrançoisFabre I have added shell=True and i got CompletedProcess(args=["adb, shell getprop | grep -e 'bt.name'"], returncode=1) but it still does not store the output
– Wojtek T
1 hour ago












2 Answers
2






active

oldest

votes

















up vote
7
down vote



accepted










The issues here:



  • passing arguments like this (string in a list, with spaces) is really not recommended

  • passing arguments like this need shell=True for it to have a slight chance to work, and shell=True is known for security issues (and other issues as well, like non-portability)


  • grep is not standard on windows

  • when not found grep returns 1 and would make check_output fail.

I'd rewrite this:



PlatformName = subprocess.check_output(["adb shell getprop | grep -e 'bt.name'"])


as:



output = subprocess.check_output(["adb","shell","getprop"])
platform_name = next((line for line in output.decode().splitlines() if "bt.name" in line),"")


The second line is a "native" version of grep (without regexes). It returns the first occurrence of "bt.line" in the output lines or empty string if not found. You don't need grep here. And your clients may not have grep installed on Windows.






share|improve this answer






















  • Thanks for the answer i tried to run your code but I am getting an AttributeError: 'list' object has no attribute 'decode'
    – Wojtek T
    1 hour ago










  • sorry, inverted both calls. Can you retry?
    – Jean-François Fabre
    1 hour ago










  • Working like a charm.
    – Wojtek T
    1 hour ago

















up vote
0
down vote













Hey I got the same problem as you. Sub-process can do what you want even with the shell=False. The trick is the communicate() method.



with subprocess.Popen(cmdCode,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd = workingDir,
bufsize=1,
universal_newlines = True) as proc:
#output is stored in proc.stdout
#errors are stored in proc.stderr


Now you just need a little function to scan the proc.stdout for the information you need: PlatformName, etc






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',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    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%2f52363569%2fstore-filtered-output-of-cmd-command-in-a-variable%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    7
    down vote



    accepted










    The issues here:



    • passing arguments like this (string in a list, with spaces) is really not recommended

    • passing arguments like this need shell=True for it to have a slight chance to work, and shell=True is known for security issues (and other issues as well, like non-portability)


    • grep is not standard on windows

    • when not found grep returns 1 and would make check_output fail.

    I'd rewrite this:



    PlatformName = subprocess.check_output(["adb shell getprop | grep -e 'bt.name'"])


    as:



    output = subprocess.check_output(["adb","shell","getprop"])
    platform_name = next((line for line in output.decode().splitlines() if "bt.name" in line),"")


    The second line is a "native" version of grep (without regexes). It returns the first occurrence of "bt.line" in the output lines or empty string if not found. You don't need grep here. And your clients may not have grep installed on Windows.






    share|improve this answer






















    • Thanks for the answer i tried to run your code but I am getting an AttributeError: 'list' object has no attribute 'decode'
      – Wojtek T
      1 hour ago










    • sorry, inverted both calls. Can you retry?
      – Jean-François Fabre
      1 hour ago










    • Working like a charm.
      – Wojtek T
      1 hour ago














    up vote
    7
    down vote



    accepted










    The issues here:



    • passing arguments like this (string in a list, with spaces) is really not recommended

    • passing arguments like this need shell=True for it to have a slight chance to work, and shell=True is known for security issues (and other issues as well, like non-portability)


    • grep is not standard on windows

    • when not found grep returns 1 and would make check_output fail.

    I'd rewrite this:



    PlatformName = subprocess.check_output(["adb shell getprop | grep -e 'bt.name'"])


    as:



    output = subprocess.check_output(["adb","shell","getprop"])
    platform_name = next((line for line in output.decode().splitlines() if "bt.name" in line),"")


    The second line is a "native" version of grep (without regexes). It returns the first occurrence of "bt.line" in the output lines or empty string if not found. You don't need grep here. And your clients may not have grep installed on Windows.






    share|improve this answer






















    • Thanks for the answer i tried to run your code but I am getting an AttributeError: 'list' object has no attribute 'decode'
      – Wojtek T
      1 hour ago










    • sorry, inverted both calls. Can you retry?
      – Jean-François Fabre
      1 hour ago










    • Working like a charm.
      – Wojtek T
      1 hour ago












    up vote
    7
    down vote



    accepted







    up vote
    7
    down vote



    accepted






    The issues here:



    • passing arguments like this (string in a list, with spaces) is really not recommended

    • passing arguments like this need shell=True for it to have a slight chance to work, and shell=True is known for security issues (and other issues as well, like non-portability)


    • grep is not standard on windows

    • when not found grep returns 1 and would make check_output fail.

    I'd rewrite this:



    PlatformName = subprocess.check_output(["adb shell getprop | grep -e 'bt.name'"])


    as:



    output = subprocess.check_output(["adb","shell","getprop"])
    platform_name = next((line for line in output.decode().splitlines() if "bt.name" in line),"")


    The second line is a "native" version of grep (without regexes). It returns the first occurrence of "bt.line" in the output lines or empty string if not found. You don't need grep here. And your clients may not have grep installed on Windows.






    share|improve this answer














    The issues here:



    • passing arguments like this (string in a list, with spaces) is really not recommended

    • passing arguments like this need shell=True for it to have a slight chance to work, and shell=True is known for security issues (and other issues as well, like non-portability)


    • grep is not standard on windows

    • when not found grep returns 1 and would make check_output fail.

    I'd rewrite this:



    PlatformName = subprocess.check_output(["adb shell getprop | grep -e 'bt.name'"])


    as:



    output = subprocess.check_output(["adb","shell","getprop"])
    platform_name = next((line for line in output.decode().splitlines() if "bt.name" in line),"")


    The second line is a "native" version of grep (without regexes). It returns the first occurrence of "bt.line" in the output lines or empty string if not found. You don't need grep here. And your clients may not have grep installed on Windows.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 1 hour ago

























    answered 1 hour ago









    Jean-François Fabre

    91.7k847102




    91.7k847102











    • Thanks for the answer i tried to run your code but I am getting an AttributeError: 'list' object has no attribute 'decode'
      – Wojtek T
      1 hour ago










    • sorry, inverted both calls. Can you retry?
      – Jean-François Fabre
      1 hour ago










    • Working like a charm.
      – Wojtek T
      1 hour ago
















    • Thanks for the answer i tried to run your code but I am getting an AttributeError: 'list' object has no attribute 'decode'
      – Wojtek T
      1 hour ago










    • sorry, inverted both calls. Can you retry?
      – Jean-François Fabre
      1 hour ago










    • Working like a charm.
      – Wojtek T
      1 hour ago















    Thanks for the answer i tried to run your code but I am getting an AttributeError: 'list' object has no attribute 'decode'
    – Wojtek T
    1 hour ago




    Thanks for the answer i tried to run your code but I am getting an AttributeError: 'list' object has no attribute 'decode'
    – Wojtek T
    1 hour ago












    sorry, inverted both calls. Can you retry?
    – Jean-François Fabre
    1 hour ago




    sorry, inverted both calls. Can you retry?
    – Jean-François Fabre
    1 hour ago












    Working like a charm.
    – Wojtek T
    1 hour ago




    Working like a charm.
    – Wojtek T
    1 hour ago












    up vote
    0
    down vote













    Hey I got the same problem as you. Sub-process can do what you want even with the shell=False. The trick is the communicate() method.



    with subprocess.Popen(cmdCode,
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    cwd = workingDir,
    bufsize=1,
    universal_newlines = True) as proc:
    #output is stored in proc.stdout
    #errors are stored in proc.stderr


    Now you just need a little function to scan the proc.stdout for the information you need: PlatformName, etc






    share|improve this answer
























      up vote
      0
      down vote













      Hey I got the same problem as you. Sub-process can do what you want even with the shell=False. The trick is the communicate() method.



      with subprocess.Popen(cmdCode,
      stdin=subprocess.PIPE,
      stdout=subprocess.PIPE,
      stderr=subprocess.PIPE,
      cwd = workingDir,
      bufsize=1,
      universal_newlines = True) as proc:
      #output is stored in proc.stdout
      #errors are stored in proc.stderr


      Now you just need a little function to scan the proc.stdout for the information you need: PlatformName, etc






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        Hey I got the same problem as you. Sub-process can do what you want even with the shell=False. The trick is the communicate() method.



        with subprocess.Popen(cmdCode,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        cwd = workingDir,
        bufsize=1,
        universal_newlines = True) as proc:
        #output is stored in proc.stdout
        #errors are stored in proc.stderr


        Now you just need a little function to scan the proc.stdout for the information you need: PlatformName, etc






        share|improve this answer












        Hey I got the same problem as you. Sub-process can do what you want even with the shell=False. The trick is the communicate() method.



        with subprocess.Popen(cmdCode,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        cwd = workingDir,
        bufsize=1,
        universal_newlines = True) as proc:
        #output is stored in proc.stdout
        #errors are stored in proc.stderr


        Now you just need a little function to scan the proc.stdout for the information you need: PlatformName, etc







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 1 hour ago









        Sharku

        37411




        37411



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52363569%2fstore-filtered-output-of-cmd-command-in-a-variable%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery