How can I tell subprocess to stop escaping my quot

2019-06-22 07:41发布

问题:

I'm trying to call Vim using subprocess, and pass it an argument. For example:

subprocess.call(['gvim.exe', ''' "+map <F5> :echo 'Hello!'<cr>" '''])

This command works on the command line:

> gvim.exe "+map <F5> :echo 'Hello!'<cr>"

And then I hit F5 and it tells me hello.

The subprocess call doesn't work. When I look at the process in task manager, I see that my string is now:

"\" +map <F5> :echo 'Hello!'<cr>\""

Not at all what I expected, and I don't think it's what Vim expects, either. It looks like subprocess is somehow escaping my quotes, but I don't know why.

Is there any way I can get this to work like I expect?

回答1:

The quotes aren't necessary since subprocess.call passes the arguments directly to the process without using the shell (unless you set shell=True).

So, subprocess.call(['gvim.exe', "+map <F5> :echo 'Hello!'<cr>"]) will suffice.