我有一个脚本,通过特定的文件夹梳理,发现所有今天被修改了的文件:写红宝石输出到文件
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