我有一個腳本,通過特定的文件夾梳理,發現所有今天被修改了的文件:寫紅寶石輸出到文件
Dir.glob("/path/to/folder/*/*.txt") do |file|
f = File.open(file.strip)
lines = f.readlines
mod = f.mtime
modtime = f.mtime.strftime("%I:%M%p")
text = lines.join
wordcount = text.split.length
project = File.basename(file).gsub(/.txt/, ' ').strip
if mod > (Time.now - 86400)
found_completed = true
entry = "#{modtime} - #{project} - #{wordcount}"
end
if found_completed == false
puts "not today"
end
if found_completed == true
puts "worked on #{entry}"
end
end
這一切工作正常。但是,我也去寫多行輸出到一個文件。當我將其添加到腳本的末尾(在最後的'結束'之前)它會出現空白:
open('/path/to/newfile.txt', 'w') { |f|
f.puts ("#{entry}" + "/n/n") }
任何幫助,將不勝感激。
這看起來非常接近。但是,如果我使用建議2,則只有在glob搜索中存在多個肯定的情況下才能獲得最後一個條目。在建議1上,我仍然獲得#{entry}的空白輸出。 – craigeley
更改爲模式'a +' –
我認爲DGM的答案也與此有關。將模式更改爲a +,但是現在我得到的空白行不符合「if」條件。有沒有辦法結束glob循環,仍然通過#{entrytext}?或消除空白行? – craigeley