当命令有双引号时,如何从python运行windows命令行命令

2024-09-20 17:33:50 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我试图运行的代码

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)

打印输出(final\u cmd)为

java-jar C:\TOOLS\u UI\syntaxchecker\设备编辑器.jarC:\Users\patela28\Perforce\content dev\dev\envision\content\content data\symantecav-v2>;“C:\Users\patela28\Desktop\jira\ESU\105\Sprint\u 27\SMC-112\ReviewDocs&;Checklist\syntaxchecker\u orig_输出.txt““

此命令确实运行,但整个输出显示在命令行上,不会重定向到syntaxchecker\u orig_输出.txt. 你知道吗

当我在命令行上复制粘贴上面的相同命令时,它工作得非常好,我得到了一个syntaxchecker\ u orig_输出.txt文件的位置。你知道吗

不知道为什么会这样。你知道吗


Tags: pathdevtxtcmdoutputosjavacontent
2条回答

不知道是什么原因

final\u cmd=cmd+'+'>;'+''+输出路径

final\u cmd=cmd+“”+'>;'+输出路径

为我工作。你知道吗

你必须启动命令处理器。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)

相关问题 更多 >

    热门问题