0

我有一个包含很多 PGN 国际象棋文件的目录,我想从中删除移动时间(写为[%emt {a_number}]。我写了这个脚本:

regex = /\[.emt[^\]]+\]/
directory = "path/to/files"
extension = ".pgn"

Dir.chdir(directory)
Dir.foreach(directory) do |file_name|
    file_object = File.open(file_name, "r+")
    contents = file_object.read
    new_contents = contents.gsub(regex, "")
    File.truncate(directory + "/" + file_name, 0)
    file_object.puts(new_contents)
    file_object.close
end

这删除了所有的移动时间,但奇怪的是它在文件的开头附加了大量的空字符(我怀疑这个数字等于文件中的字节数)。所以我用 替换了这一行new_contents = contents.gsub(regex, "")contents.delete("\0")但这只会让情况变得更糟,在文件的开头附加了更多的空字符。我怎样才能删除它们?

4

2 回答 2

1

如果您更换它应该可以正常工作:

File.truncate(directory + "/" + file_name, 0)

和:

file_object.rewind

或者

file_object.seek(0)

File.truncate不应应用于打开的文件(如此处),并且file_object.truncate不应跟随除 . 之外的任何文件操作file_object.close

如果您已经有一个要删除的带有空值的文件,请将文件读入字符串str,关闭文件,执行

str.delete!("\000")

然后写str回文件。

于 2014-08-01T06:44:09.580 回答
0

Instead of truncating the file, better just re-open it for writing instead as it would automatically be truncated with it. And I believe you missed specifying the proper paths:

file_path = File.join(directory, file_name)
contents = File.read(file_path)  ## Previously just file_name.
new_contents = contents.gsub(regex, "")
File.open(file_path, 'w') do |file_object|
    file_object.puts(new_contents)
end

Also perhaps you didn't want to use puts as somehow it's different when writing binary data compared to writing ascii data:

File.write(file_path)
于 2014-08-01T06:52:39.303 回答