Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 失败的exec。文件名为的命令包含两个空格_Windows_Go_Explorer - Fatal编程技术网

Windows 失败的exec。文件名为的命令包含两个空格

Windows 失败的exec。文件名为的命令包含两个空格,windows,go,explorer,Windows,Go,Explorer,我想用浏览器从Go的exec.Command()打开包含两个空格的文件 此命令在Windows PowerShell中正常工作 Explorer "file://C:\Users\1. Sample\2. Sample2" 使用Go的exec.Command()可以处理文件名中包含的如下空格 exec.Command(`explorer`, "file://C:\Users\1. Sample").CombinedOutput() 但失败,文件名包含两个空格,如下所示 exec.Comma

我想用浏览器从Go的
exec.Command()
打开包含两个空格的文件

此命令在Windows PowerShell中正常工作

Explorer "file://C:\Users\1. Sample\2.  Sample2"
使用Go的
exec.Command()
可以处理文件名中包含的如下空格

exec.Command(`explorer`, "file://C:\Users\1. Sample").CombinedOutput()
但失败,文件名包含两个空格,如下所示

exec.Command(`explorer`, "file://C:\Users\1. Sample\2.  Sample2").CombinedOutput()
请告诉我怎么解决这个问题


此代码按预期工作。谢谢

exec.Command(`explorer`, `file://C:\Users\1. Sample\2.  Sample2`).CombinedOutput()
但实际输入是字符串(不是原始字符串文字),如下所示。所以我认为我需要将字符串转换为原始字符串

url := "file://C:\Users\1. Sample\2.  Sample2"

       <I need to convert to raw string literal?> 

result, err := exec.Command(`explorer`, url).CombinedOutput()

if err != nil {
    log.Fatal(err)
}
url:=“文件:://C:\Users\1.Sample\2.Sample2"
结果,err:=exec.Command(`explorer`,url).CombinedOutput()
如果错误!=零{
log.Fatal(错误)
}

文件名中有2个空格与无法运行命令无关

您正在使用:

在解释的字符串文字中,反斜杠字符
\
是特殊的,如果希望字符串
有反斜杠,则必须对其进行转义,转义序列为2个反斜杠:
\

"file://C:\\Users\\1. Sample\\2.  Sample2"
或者更好:使用(这样你就不需要逃避):

那奇怪的
字符也可能会导致问题。如果在Go源代码中包含该字符,它将被解释并编码为UTF-8字符串。您可以将其作为参数传递,但您的操作系统(windows)在检查文件名时可能会使用不同的字符编码,因此可能找不到它。请删除该奇怪的
文件名中的字符

当您从PowerShell运行该命令时,它会起作用,因为PowerShell不需要解释字符串,并且它使用的编码也用于存储文件名


还有(就像和)返回一个
错误
,一定要检查它,因为它可能会提供额外的信息。

在进行windows系统调用时不应该正确地翻译编码?您有什么建议吗?@gaku是的,使用原始字符串文字并从文件名中删除奇怪的字符。这些在response.hmm..exec.Command中提到(
explorer
,“file://C:\Users\1.Sample\2.Sample”)起作用,但exec.Command(
explorer
,“file://C:\Users\1.Sample\2.Sample”)不起作用。字符串文字无法转换为原始字符串文字吗?
"file://C:\\Users\\1. Sample\\2.  Sample2"
`file://C:\Users\1. Sample\2.  Sample2`