系统:
Windows Server 2008 Ruby 192
在'删除'中:权限被拒绝Errno:EACCES
行:File.delete('filename.ext')
如果您知道任何其他方法删除文件绕过此错误我很高兴为您分发一些点:)我的意思是任何帮助将不胜感激;-p
我怀疑文件没有关闭,但它已关闭.
源代码:
Dir.foreach(FileUtils.pwd()) do |f|
a[i] = f
if a[i].end_with?('log')
file = File.open(a[i])
if file.ctime < TIME_TO_REMOVE_LOGS || file.mtime < TIME_TO_REMOVE_LOGS || File.size(a[i]) > MAX_FILE_SIZE
puts a[i]
puts file.ctime
puts file.mtime
# zipping the file
orig = a[i]
Zlib::GzipWriter.open('arch_log.gz') do |gz|
gz.mtime = File.mtime(orig)
gz.orig_name = orig
gz.write IO.binread(orig)
end
file.close
end
end
File.delete(a[i])
i = i + 1
end
Run Code Online (Sandbox Code Playgroud)
pgu*_*rio 14
它没有关闭.该文件有时会根据ctime关闭.您的代码存在很多问题,但重点是:对文件安全并使用块.
File.open(a[i]) do |file|
# access file
end
# now you don't need to wonder if it's closed.
File.delete(a[i])
Run Code Online (Sandbox Code Playgroud)
同时为变量提供更好的名称.
| 归档时间: |
|
| 查看次数: |
13417 次 |
| 最近记录: |