Ruby(Errno :: EACCES)在File.delete上 [英] Ruby (Errno::EACCES) on File.delete

查看:128
本文介绍了Ruby(Errno :: EACCES)在File.delete上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我使用完一些XML文件后,我正试图删除它们,其中一个给我这个错误:

I am trying to delete some XML files after I have finished using them and one of them is giving me this error:

'delete': Permission denied - monthly-builds.xml (Errno::EACCES)

Ruby声称该文件受写保护,但是在尝试删除它之前我先设置了权限.

Ruby is claiming that the file is write protected but I set the permissions before I try to delete it.

这就是我想要做的:

#collect the xml files from the current directory
filenames = Dir.glob("*.xml")

#do stuff to the XML files
finalXML = process_xml_files( filenames )

#clean up directory
filenames.each do |filename|
        File.chmod(777, filename) # Full permissions
        File.delete(filename)
end

有什么想法吗?

推荐答案

此:

File.chmod(777, filename)

不执行您认为的操作.从精细手册:

doesn't do what you think it does. From the fine manual:

将命名文件上的许可位更改为mode_int表示的位模式.

Changes permission bits on the named file(s) to the bit pattern represented by mode_int.

强调我的.文件模式通常以八进制指定,因为它很好地将位分成了三个Unix权限组(所有者,组和其他):

Emphasis mine. File modes are generally specified in octal as that nicely separates the bits into the three Unix permission groups (owner, group, other):

File.chmod(0777, filename)

因此,您实际上并没有将文件设置为完全访问权限,而是将权限位设置为01411,如下所示:

So you're not actually setting the file to full access, you're setting the permission bits to 01411 which comes out like this:

-r----x--t

而不是

-rwxrwxrwx

您期望的

.请注意,您的(十进制)777许可权位图已删除写许可权.

that you're expecting. Notice that your (decimal) 777 permission bitmap has removed write permission.

此外,要删除文件,需要对该文件所在目录的写权限(至少在Unixish系统上),因此请检查该目录的权限.

Also, deleting a file requires write access to the directory that the file is in (on Unixish systems at least) so check the permissions on the directory.

最后,您可能要检查 File.chmod :

And finally, you might want to check the return value from File.chmod:

[...]返回已处理的文件数.

[...] Returns the number of files processed.

仅仅是因为您调用并不意味着它将成功.

Just because you call doesn't mean that it will succeed.

这篇关于Ruby(Errno :: EACCES)在File.delete上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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