Search code examples
pythonpipepopen

How to handle ':' in command using os.popen


I am trying to call a program with:

os.popen("program -s:'*' -c:'A;B;C;'")

However, it seems that it was interpreted as shell command:

program -s '*' -c 'A;B;C;'

which result incorrect behavior.

Can somebody help me on how to hanle such situdations where ':' is inside shell commandline?


Solution

  • Don't use os.popen(), use the subprocess module instead:

    import subprocess
    
    result = subprocess.check_output(['program', "-s:'*'", "-c:'A;B;C;'"])
    

    This returns the output of the program without running it through a shell, passing in the arguments directly without any additional parsing.