简体   繁体   English

当命令带有双引号时如何从python运行Windows命令行命令

[英]How to run a windows command line command from python when the command has double quotes

Following is the code I am trying to run 以下是我尝试运行的代码

device_editor_path = os.path.join(syntax_checker_path,'DeviceEditor.jar')
output_path = os.path.join(reviewdocs_path,'syntaxchecker_orig_output.txt')
output_path = '"%s"' % output_path # Need to do this because in case there is a space in output_path
# run syntax checker
cmd = 'java -jar' + ' ' + device_editor_path + ' ' + content_data_path + ' ' + event_source_name
if version == 'v2':
    cmd = cmd + ' ' + '-v2'
final_cmd = cmd + ' ' + '>' + ' ' + output_path
# final_cmd_test = r'java -jar C:\TOOLS_UI\syntaxchecker\DeviceEditor.jar C:\Users\patela28\Perforce\content-dev\dev\envision\content\content-data\ symantecav -v2 > "C:\Users\patela28\Desktop\jira\ESU#105\Sprint_27\SMC-112\ReviewDocs&Checklist\syntaxchecker_orig_output.txt"'
print(final_cmd)
status = os.system(final_cmd)

The output of print(final_cmd) is print(final_cmd)的输出是

java -jar C:\\TOOLS_UI\\syntaxchecker\\DeviceEditor.jar C:\\Users\\patela28\\Perforce\\content-dev\\dev\\envision\\content\\content-data\\ symantecav -v2 > "C:\\Users\\patela28\\Desktop\\jira\\ESU#105\\Sprint_27\\SMC-112\\ReviewDocs&Checklist\\syntaxchecker_orig_output.txt" java -jar C:\\ TOOLS_UI \\ syntaxchecker \\ DeviceEditor.jar C:\\ Users \\ patela28 \\ Perforce \\ content-dev \\ dev \\ envision \\ content \\ content-data \\ symantecav -v2>“ C:\\ Users \\ patela28 \\ Desktop \\ JIRA \\ ESU#105 \\ Sprint_27 \\ SMC-112 \\ ReviewDocs&清单\\ syntaxchecker_orig_output.txt”

This command does run but the entire output shown on the command line and is not getting redirected to syntaxchecker_orig_output.txt. 该命令可以运行,但是整个输出显示在命令行上,并且不会重定向到语法checker_orig_output.txt。

When I copy paste the same above command on the command line it works perfectly and I get a syntaxchecker_orig_output.txt file at the location. 当我在命令行上复制粘贴上面相同的命令时,它可以正常工作,并且在该位置得到了一个语法checker_orig_output.txt文件。

Not able to figure out why this is happening. 无法弄清楚为什么会这样。

You have to start the command processor. 您必须启动命令处理器。 Java won't parse for you the command line. Java不会为您解析命令行。 The following should work: 以下应该工作:

device_editor_path = os.path.join(syntax_checker_path,'DeviceEditor.jar')
output_path = os.path.join(reviewdocs_path,'syntaxchecker_orig_output.txt')
output_path = '"%s"' % output_path # Need to do this because in case there is a space in output_path
# run syntax checker
cmd = 'cmd.exe /c java -jar' + ' ' + device_editor_path + ' ' + content_data_path + ' ' + event_source_name
if version == 'v2':
    cmd = cmd + ' ' + '-v2'
final_cmd = cmd + ' ' + '>' + ' ' + output_path
# final_cmd_test = r'java -jar C:\TOOLS_UI\syntaxchecker\DeviceEditor.jar C:\Users\patela28\Perforce\content-dev\dev\envision\content\content-data\ symantecav -v2 > "C:\Users\patela28\Desktop\jira\ESU#105\Sprint_27\SMC-112\ReviewDocs&Checklist\syntaxchecker_orig_output.txt"'
print(final_cmd)
status = os.system(final_cmd)

Don't know the reason but changing 不知道原因,只是改变

final_cmd = cmd + ' ' + '>' + ' ' + output_path final_cmd = cmd +''+'>'+''+ output_path

to

final_cmd = cmd + ' ' + '>' + output_path final_cmd = cmd +''+'>'+ output_path

worked for me. 为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM