[英]Cannot use File.delete to remove file
系統:
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
它沒有關閉。 該文件有時會根據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])
同時為變量提供更好的名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.