在 Ruby 中使用 fileutils 重命名目录
在重命名一组目录时,我最终遇到了奇怪的错误 我成功地为许多文件夹和子文件夹调用了 FileUtils。
但是当我在子子文件夹上尝试它时,我收到一个奇怪的错误。 在执行 FileUtils.mv 之前,我打印 src 和 dest 以确保它们代表正常的内容,这就是我最终得到的结果:
change Folder_From_Archive/Student1/Ex1/
to Folder_From_Archive/Student1/ex1/
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/fileutils.rb:505:in `rename': Invalid argument - Folder_From_Archive/Student/Ex1/ or Folder_From_Archive/Student/ex1/Ex1 (Errno::EINVAL)
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/fileutils.rb:505:in `mv'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/fileutils.rb:1396:in `fu_each_src_dest'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/fileutils.rb:1410:in `fu_each_src_dest0'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/fileutils.rb:1394:in `fu_each_src_dest'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/fileutils.rb:494:in `mv'
from unzip_all.rb:51:in `verify_submissions'
from unzip_all.rb:44:in `each'
from unzip_all.rb:44:in `verify_submissions'
from unzip_all.rb:40:in `each'
from unzip_all.rb:40:in `verify_submissions'
from unzip_all.rb:80
有趣的是,我有许多 Student 文件夹,我使用相同的命令成功重命名了它们。
编辑:按照问题。 我尝试将文件夹 path/Ex1 移动到 path/ex1 我基本上想将Ex1重命名为ex1,所以在运行之前ex1不存在。
以下是我可以重命名的文件夹的 ls 结果 drwxr-xr-x 这是我无法重命名的文件夹 Ex1 drwxr-----
While renaming a set of directories I end up wit ha strange error
I call FileUtils with success for many folders, and subfolders.
But when I try it on subsubfolder I get a strange error.
Just before executing FileUtils.mv I print src and dest to be sure that they represent something ok and this is what I end up with :
change Folder_From_Archive/Student1/Ex1/
to Folder_From_Archive/Student1/ex1/
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/fileutils.rb:505:in `rename': Invalid argument - Folder_From_Archive/Student/Ex1/ or Folder_From_Archive/Student/ex1/Ex1 (Errno::EINVAL)
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/fileutils.rb:505:in `mv'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/fileutils.rb:1396:in `fu_each_src_dest'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/fileutils.rb:1410:in `fu_each_src_dest0'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/fileutils.rb:1394:in `fu_each_src_dest'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/fileutils.rb:494:in `mv'
from unzip_all.rb:51:in `verify_submissions'
from unzip_all.rb:44:in `each'
from unzip_all.rb:44:in `verify_submissions'
from unzip_all.rb:40:in `each'
from unzip_all.rb:40:in `verify_submissions'
from unzip_all.rb:80
What is funny is that I have many Student folder that I rename sucessfully with the same command.
EDIT : Following the questions.
I try to move the folder path/Ex1 to path/ex1
I want to rename Ex1 to ex1 basically, so before running it ex1 does not exist.
Here are the result of ls for the folders that I can rename
drwxr-xr-x
And here it is for the folder Ex1 that I can't rename
drwxr-----
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您没有发布代码,所以很难确定出了什么问题,但错误消息清楚地表明您正在尝试将 Ex1 目录移动到不存在的 ex1 目录中。您需要修复对
File.mv
的调用。It's hard to be sure what is going wrong since you didn't post your code, but the error message clearly shows that you are trying to move the Ex1 directory into the ex1 directory, which doesn't exist. You need to fix your call to
File.mv
.您可以使用“ls -ld”检查这些文件夹的权限和所有权,并将其发布到此处吗?例如包含的文件夹和导致错误的文件夹
can youcheck with 'ls -ld' what the permissions and ownership of those folders are, and post them here? e.g. the enclosing folder and the folder causing the error