Python subprocess execute command with \ in command string
Question: I'm writing a simple program that takes in a command line string and executes
it.

An example command line string could be dir "c:\users\xxx\My Documents"

I'm having trouble trying to execute this due to the '\'. I've specified a dir
name as an example of a string that has a backslash in it.

Since the command I receive could be anything - say regex's etc, the \ should
be maintained. How would I ensure that the user input stays as entered in the
python script?

    
    
    import platform
    import subprocess
    import sys
    
    if __name__ == '__main__':
        command = sys.argv[0].split()
    
        if platform.system().lower() == 'windows':
            runShell = True
    
        out = subprocess.Popen(command,shell=runShell,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    
        for line in out.stdout.readlines(): 
            print line.strip()
    


Answer: So I found the answer on how to parse shell commands provided as a string to a
py module.

Use the shlex module - <https://docs.python.org/2/library/shlex.html> to split
the command str into a list that can be executed by the subprocess.Popen
method.

