必须在 Windows 7 上运行 ruby 脚本并获得权限被拒绝 EACCES [英] Have to run ruby script on Windows 7 and got Permission denied EACCES
问题描述
我必须在 Windows 7 上运行 ruby 脚本(我知道这是个坏主意).我的脚本创建文件夹(如果它们不存在)并将文件复制到其中.我正在使用 FileUtils lib 来执行这项工作,例如:
I have to run ruby script on windows 7 (I know that it's bad idea). My script creates folders (if they is not exist) and copies files into them. I'm using FileUtils lib to perform this job, like:
FileUtils.mkdir_p(path)
FileUtils.cp_r(file.path, path)
FileUtils.touch(file)
在 ubuntu 和 mac 上一切正常,但在 Windows 7 机器上我遇到了下一个错误:
On ubuntu and mac everything is ok, but on windows 7 machine I got next error:
Permission denied - ./program_folder/input/. (Errno::EACCES)
在此代码行上:
Dir.entries('./program_folder/input').map { |file_name| File.new("./program_folder/input/#{file_name}") }.compact
有什么想法可以解决吗?
Any ideas how can I fix it?
我尝试以管理员访问权限运行 ruby 和 irb termianl 并尝试在所有路径上执行 FileUtils.chmod_R(0777, @path) 但仍然没有任何更改...
I have tried to run ruby and irb termianl with administrator access and tried to do FileUtils.chmod_R(0777, @path) on all paths but still no changes...
推荐答案
你的命令
Dir.entries('./program_folder/input').map { |file_name|
File.new("./program_folder/input/#{file_name}")
}.compact
尝试创建一个与您之前阅读的文件/文件夹同名的文件.
tries to create a File with the same name as the file/folder you read before.
详细说明:
Dir.entries('.')
找到的第一个文件是实际目录 (.
)."./program_folder/input/#{file_name}"
是./program_folder/input/.
(现有目录).- 这个目录路径应该是一个新文件的路径.
- 使用
File.new
,您无法将目录作为文件打开.
- The first file found by
Dir.entries('.')
is the actual directory (.
). "./program_folder/input/#{file_name}"
is./program_folder/input/.
(an existing directory).- This directory path should be the path of a new file.
- With
File.new
you can't open a directory as a File.
<小时>
评论后备注:
Remark after comment:
在 Dir.entries
中调用 File.new
- 创建文件句柄.如果没有模式,它会尝试打开现有文件(文件,而不是目录!)..
是一个不能作为文件打开的目录.
Inside the Dir.entries
you call File.new
- that creates a file handle. Without a mode, it tries to open an existing File (File, not Directory!). .
is a directory which can't be opended as a file.
如果您只想要文件名,则不需要 File.new
,字符串 "./program_folder/input/#{file_name}"
将是足够的.更好的解决方案是 File.join
方法:
If you only want the filename, you don't need the File.new
, the string "./program_folder/input/#{file_name}"
would be enough.
A better solution would be the File.join
method:
File.join("./program_folder/input", file_name)
或
File.join(".", "program_folder", "input", file_name)
如果你需要真实的文件名,你可以检查目录:
If you need on real filename, you can check for directories:
Dir.entries('./program_folder/input').map { |file_name|
"./program_folder/input/#{file_name}" unless File.directory?("./program_folder/input/#{file_name}")
}.compact
或者更好的是,您删除目录:
or better, you remove the directories:
Dir.entries('.').delete_if{|file_name|
File.directory?(file_name)
}
这篇关于必须在 Windows 7 上运行 ruby 脚本并获得权限被拒绝 EACCES的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!