FileUtils.mv 抛出 Invalid char \302 和 \255 异常 [英] FileUtils.mv throwing Invalid char \302 and \255 exception

查看:55
本文介绍了FileUtils.mv 抛出 Invalid char \302 和 \255 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 CodeRunner 或命令行中运行以下代码时,出现以下错误;

Untitled.rb:25: Invalid char '\302' in expressionUntitled.rb:25: 表达式中的字符 '\255' 无效

第 25 行对应于带有 FileUtils.mv 方法的行.

if the_file.include?截屏"old_file = Shellwords.escape(homepath + "/Desktop/" + the_file)new_file = Shellwords.escape(screen_shot_folder + "/" + the_file)FileUtils.mv old_file, new_file结尾

最初我认为我的输入(old_filenew_file)有问题,这就是我将它们包装在 Shellwords.escape 我还检查了 old_filenew_file 的内容是否正确.

如果我调用 puts old_file + " ++ " + new_file 我得到如下内容;

/Users/tobyosbourn/Desktop/Scr​​een\ Shot\ 2013-02-04\ at\ 10.49.14\ \(2\).png ++/Users/tobyosbourn/Desktop/Scr​​eenshots/Screen\ Shot\ 2013-02-04\ at\ 10.49.14\ \(2\).png

看起来不错,但如果我将它们作为字符串复制并粘贴到 FileUtils.mv 中,我会收到相同的错误消息.

我有 阅读 FileUtils 的用途 并没有发现我做错了什么,有人可以建议吗?

如果它有助于我的 Ruby 版本是 1.8.7

解决方案

您的源代码中有一个不可见的非 ASCII 字符,可能是通过从某个网站上复制粘贴代码添加的.

具体来说,您的代码中有一个恶意软连字符,编码为 UTF-8(软连字符 = UTF-8 中的 C2 AD,或八进制转义符号中的 \302\255).

最好的做法是在您的文本编辑器中显示不可见",看看您是否能找到与其他空间不同的空间.

EDIT 将您的代码段粘贴到我的文本编辑器中会在 FileUtils. 之后(在 .mv<之间)显示一个软连字符/代码>).删除 FileUtils.mv 并重新键入它以删除您看不到的字符.

When I run the below code in CodeRunner or from the command line I get the following error;

Untitled.rb:25: Invalid char '\302' in expression
Untitled.rb:25: Invalid char '\255' in expression

Line 25 corresponds to the line with the FileUtils.mv method.

if the_file.include? "Screen Shot"
    old_file = Shellwords.escape(homepath + "/Desktop/" + the_file)
    new_file = Shellwords.escape(screen_shot_folder + "/" + the_file)

    FileUtils.­mv old_file, new_file
end

Initially I thought there was something wrong with my input (either old_file or new_file) which is why I have wrapped them in Shellwords.escape I have also checked that the contents of old_file and new_file are correct.

If I call puts old_file + " ++ " + new_file I get something like the following;

/Users/tobyosbourn/Desktop/Screen\ Shot\ 2013-02-04\ at\ 10.49.14\ \(2\).png ++ /Users/tobyosbourn/Desktop/Screenshots/Screen\ Shot\ 2013-02-04\ at\ 10.49.14\ \(2\).png

Which looks spot on but if I copy and paste those as strings into FileUtils.mv I get the same error message.

I have read how FileUtils is meant to be used and couldn't see anything I am doing wrong, could anyone advise?

If it helps my version of Ruby is 1.8.7

解决方案

You have an invisible non-ASCII character in your source code, likely added by copy-and-pasting code off a website somewhere.

To be specific, you've got a rogue soft-hyphen in your code, encoded as UTF-8 (soft-hyphen = C2 AD in UTF-8, or \302\255 in octal escape notation).

Best thing to do would be to "show invisibles" in your text editor and see if you can find a space that isn't like the others.

EDIT Pasting your snippet into my text editor reveals a soft-hyphen right after FileUtils. (between . and mv). Delete FileUtils.mv and retype it to get rid of the character if you can't see it.

这篇关于FileUtils.mv 抛出 Invalid char \302 和 \255 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆