ruby - How can I delete all null characters from a file? -


I had a directory with several PGN chess files, from which I wanted to take the time of the move ( [ % Emt {a_number}] I wrote this script:

  regex = / = [. Emt [^ \]] +++] directory = "path / from / files "Extension =" .pgn "Dir.chdir (directory) Dir.foreach (directory) | file_name | file_object = File.open (file_name," r + ") content = file_object.read new_contents = contents.gsub (regex," ") File.truncate (directory +" / "+ file_name, 0) file_object.puts (new_contents) file_object.close end  

This time removed all the moves But the interesting thing is that a large number of empty characters at the beginning of one of the files (I suspect that this number is the number of bytes in the file). So I used the line new_contents = contents.gsub (regex, "") with content.delete ("\ 0") , but it only makes it worse, and adds more blank characters for it too How can I remove them?

If you change, then you should work properly:

  With file.truncate (directory + "/" + file_name, 0)  

:

  file_object.rewind  
< P> or

  file_object.seek (0)  

file. Transact should not be applied to open files (as it is here), and any file operation other than file_object.truncate to file_object.close Should not be done after.

If you already have an empty file that you want to delete, then read the file in a string str , close the file, execute

  Str.delete! ("\ 000")  

then return to the str file.


Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -