0

I am uploading txt files using carrierwave. The files are not small (80 MB - 500 MB) and I want to remove some of the lines to reduce this size (about 80% of the file size is going to be reduced).

I have created a model method in order to clear these lines:

require 'fileutils'

def clear_unnecessary_lines
  old_file_path = Rails.root.join('public').to_s + log_file.to_s
  new_file_path = old_file_path.sub! '.txt', '_temp.txt'
  File.open(old_file_path, 'r') do |old_file|
    File.open(new_file_path, 'w') do |new_file|
      old_file.each_line do |line|
        new_file.write(line) unless line.grep(/test/).size > 0
      end
    end
  end
  FileUtils.mv new_file_path, old_file_path
end

but I am getting error when I am trying to open the new file saying there is no such file. As I have read opening a file with the w option should create an empty file for writing. Then why I am getting such error?

Also, since log_file column is holding the path to the original file, and I am changing it, could you tell how to rename the new file with the old name? As I have checked I should specify only old and new names, not paths.

Errno::ENOENT: No such file or directory - /home/gotqn/Rails/LogFilesAnalyser/LogFilesAnalyser/public/uploads/log_file/log_file/3/log_debug_temp.txt

It is strange that If I execute the following command in rails console, it is not throwing an error and the file is created.

File.open('/home/gotqn/Rails/LogFilesAnalyser/LogFilesAnalyser/public/uploads/log_file/log_file/3/log_debug_temp.txt','w')
Community
  • 1
  • 1
gotqn
  • 42,737
  • 46
  • 157
  • 243
  • Are you sure that it's the line you think it is that's causing the problem? Is it actually erroring on `File.open(old_file_path, 'r')`? File.open with the "w" option doesn't need the file to exist already but with the "r" option the file does need to exist (since it's reading it). – Max Williams Sep 05 '14 at 09:10
  • @MaxWilliams Yes, I am sure. I have commented the code about `new_file` and it is working. Also, note the error is about `log_debug_temp.txt`, where the name of the `old_file` is `log_debug.txt` – gotqn Sep 05 '14 at 09:13
  • Is the script running with necessary permissions? – BroiSatse Sep 05 '14 at 09:34
  • @BroiSatse I cannot tell. The method is called in the controller like this `@log_file.clear_unnecessary_lines` in the `create` action. Since, `carierwave` is creating files in this directory, I guess I can create files, too. – gotqn Sep 05 '14 at 09:39

1 Answers1

3

Ah, i see your problem now. When you do this

new_file_path = old_file_path.sub! '.txt', '_temp.txt'

you call the "self-altering" version of sub, ie sub!. This will actually change the value of old_file_path as a side effect. Then, in the next line, you try to open this file, which hasn't been created yet. Take out the exclamation mark and you should be fine.

Max Williams
  • 32,435
  • 31
  • 130
  • 197