繁体   English   中英

Ruby - 怎么做 IO.popen 路径中有空格

[英]Ruby - How to do IO.popen with spaces in path

我正在尝试在 Windows 上运行以下代码:

require 'rbconfig'
is_windows = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)
OUT_FILE = "test/simulation/out.txt"
File.delete OUT_FILE if File.exists? OUT_FILE 

if is_windows
    var = IO.popen("C:\\Program Files\\Microchip\\MPLABX\\v5.45/mplab_platform/bin>mdb.bat ./test/simulation/sim_instructions.txt > " + OUT_FILE)
else
    var = IO.popen("#{ENV['MPLABX_ROOT']}/mplab_ide/bin/mdb.sh ./test/simulation/sim_instructions.txt > " + OUT_FILE)
end
sleep 1
if File.exists? OUT_FILE 
    file_contents = File.read OUT_FILE
    file_contents.gsub!("/n", "")
    print file_contents
end

我不断收到错误:

'C:\Program' is not recognized as an internal or external command,

我对 Ruby 不是很熟悉,所以当我在文件路径中有空格时,有人可以帮助我如何使用 IO.popen。 或与 IO.popen 类似的其他 function。

谢谢

解决了以下解决方案,即用引号包围路径。 所以现在这是我用正确的 output 更改的行:

var = IO.popen('"C:\\Program Files\\Microchip\\MPLABX\\v5.45/mplab_platform/bin/mdb.bat" ./test/simulation/sim_instructions.txt > ' + OUT_FILE)

暂无
暂无

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

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