当命令有双引号

时间:2017-04-21 05:50:40

标签: python windows os.system

以下是我正在尝试运行的代码

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)

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:\用户\ patela28 \桌面\ JIRA \ ESU#105 \ Sprint_27 \ SMC-112 \ ReviewDocs&安培;清单\ syntaxchecker_orig_output.txt"

此命令确实运行,但命令行上显示的整个输出并未重定向到syntaxchecker_orig_output.txt。

当我在命令行上复制粘贴相同的上述命令时,它工作正常,我在该位置获得了一个syntaxchecker_orig_output.txt文件。

无法弄清楚为什么会这样。

2 个答案:

答案 0 :(得分:0)

您必须启动命令处理器。 Java不会为你解析命令行。以下应该有效:

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)

答案 1 :(得分:0)

不知道原因,但改变

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

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

为我工作。