2016-12-03 10 views
0

これは非常に基本的な質問ですが、それを特定することはできません。一重引用符をPythonの文字列で二重引用符で囲む方法

私は、スクリプトでカスタマイズされたpythonコマンドベースのコマンドを実行する必要があります。

ex: 
below is the actual commands. 
command-dist --host='server1' -- cmd -- 'command1;command2' 

私は、このコマンドのニーズが

が、私は私のスクリプトで使用するには、このコマンド形式を必要とするリモートサーバー上で実行することが、私のスクリプトを形成 私のPythonスクリプトでこのコマンドを使用する必要が

cmd="ssh -q %s "command-dist --host=%s -- cmd -- 'cmd1;cmd2'"" %(host1,host2) 

しかし、上記の引用符のために失敗している、まだ幸運の方法の数を試してみました。

When I tried this way 
cmd="ssh -q %s \"command-dist --host=%s -- cmd -- 'cmd1;cmd2'\"" %(host1,host2) 

I don't under why back slashes are appearing before and after cmd1;cmd2? 
This is what I am not getting. 
cmd 
'ssh -q %s "command-dist --host=%s -- cmd -- /'cmd1;cmd2'/""' %(host1,host2) 

あなたは内側と外側同じ引用符を使用するので、私は上記の値

答えて

0

この

cmd="ssh -q %s "command-dist --host=%s -- cmd -- 'cmd1;cmd2'"" %(host1,host2) 

cmd = <some string>command-dist --host=%s -- cmd -- 'cmd1;cmd2'<some string> 

としてPythonが理解され得るのですか助けてください。 バックスラッシュで内部引用符をエスケープする代わりに試してみてください。

cmd="ssh -q %s \"command-dist --host=%s -- cmd -- 'cmd1;cmd2'\"" %(host1,host2) 

それはさておき、あなたはsubprocessモジュールを使用してsubprocess.Popenにリストとしてあなたのコマンドを提供する必要があります。

関連する問題