#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Suprocess and varialbles


    Hello,
    I'm having trouble with getting a variable interpreted when using the suprocess module.

    Basically here's what I would like to do:
    import subprocess

    var1= "blue"
    var2= "yellow"
    process=subprocess.Popen('getcolors %var1 %var2), shell=True,stdout=subprocess.PIPE)

    print process.stdout.readline()
    My main interest is in how to get the variables "var1" and "var2" interpreted inside the subprocess.Popen statement.

    Thank you!!

    Tclark
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    I think you want:
    Code:
    command = 'getcolors {} {}'.format(var1, var2)
    process=subprocess.Popen(command, shell=True,stdout=subprocess.PIPE)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by b49P23TIvg
    I think you want:
    Code:
    command = 'getcolors {} {}'.format(var1, var2)
    process=subprocess.Popen(command, shell=True,stdout=subprocess.PIPE)
    Thank you, the syntax is tricky...

    What if I wanted to use it as follows:

    content of newfile:
    Code:
    this line has blue in it
    this line also has blue in it
    Code:
    var1="blue"
    command='cat newfile |grep -i var1 |grep -v var1'
    
    process=subprocess.Popen(command), shell=True,stdout=subprocess.PIPE)
    
    print process.stdout.readline()
    This still does not work. There is no output. However, if I hardcode this in the the Popen with the acatual values instead or varialbles it work file.

    Would appreciate any extra help.

    Thanks again
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    First, the unix command the command

    cat newfile |grep -i blue |grep -v blue

    means "Ignoring case, find all lines containing the substring `blue' except for those in which blue is lower case."

    For your input file I expect no output.



    Lastly, learn about string substitution. click here or find your own reference.


    And secondly, if you must use python the task you want, done in python, is lightweight. You won't have to create cat+grep+grep+bash at least 4 new processes.
    Code:
    import re
    
    var1 = 'blue'
    yeah = re.compile('(?i)'+var1).search
    neah = re.compile(var1).search
    
    with open('newfile') as inf:
        print(''.join(line for line in inf if yeah(line) and not neah(line)))
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by b49P23TIvg
    First, the unix command the command

    cat newfile |grep -i blue |grep -v blue

    means "Ignoring case, find all lines containing the substring `blue' except for those in which blue is lower case."

    For your input file I expect no output.



    Lastly, learn about string substitution. click here or find your own reference.


    And secondly, if you must use python the task you want, done in python, is lightweight. You won't have to create cat+grep+grep+bash at least 4 new processes.
    Code:
    import re
    
    var1 = 'blue'
    yeah = re.compile('(?i)'+var1).search
    neah = re.compile(var1).search
    
    with open('newfile') as inf:
        print(''.join(line for line in inf if yeah(line) and not neah(line)))


    Correction:
    The file should look as follows:
    Code:
    this line has blue in it
    this line also has BLUE in it
    Then there would be no output. But the point was about using the variables in the Popen() statement "not" the actual grep syntax.

    I will take another look at that section you referenced and review the tricky syntax..

    The way you proposed the solution seems overly complicated for the simple example I listed... at least for someone new to this language like myself. Seems like it could be much more straight forward short of just using bash.

    Any other suggestions regarding the variable substitution in the example I listed would be appreciated.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    I didn't claim that

    grep -i blue < source | grep -v blue

    was difficult. I claimed it is a bizarre resource hog. (at least for small source file)

    Why use python if your task is better suited to a bash script?
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by b49P23TIvg
    I didn't claim that

    grep -i blue < source | grep -v blue

    was difficult. I claimed it is a bizarre resource hog. (at least for small source file)

    Why use python if your task is better suited to a bash script?
    You keep missing the point, but Thank you!

    In any case, I appreciate your help. I was able to figure it out from your first example(...and reviewing the string formatting section, again):

    This syntax worked:
    Code:
    command='cat newfile |grep -i %s|grep -v %s' % (var1, var1)
    I will be using this in a completely different context.

    Thanks again for the different looks and ideas on this...
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Your main point was to substitute the values of python variables into strings, yes? You learned this sufficiently, yes? Please explain what main idea I missed.
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Your main point was to substitute the values of python variables into strings, yes? You learned this sufficiently, yes? Please explain what main idea I missed.
    Please disregard and Thank you for your help.

    Cheers!

IMN logo majestic logo threadwatch logo seochat tools logo