Ruby语法错误:文件复制

时间:2010-11-16 22:19:57

标签: ruby file syntax copy

编写一个小脚本来复制一些文件,但是我收到错误:

  

fileops.rb:6:语法错误,意外')'

继承我的代码

require 'ftools'

plays = ['RomeoAndJuliet.txt', 'Hamlet.txt', 'TheTempest.txt', 'TheMerchantofVenice.txt', 'AMidSummerNightsDream.txt']

plays.each do |filename|
  File.new("/clean/_cleaned" + filename, w+)
  File.syscopy(filename, "/clean/_cleaned" + filename)
end

所有括号似乎都应该是它们的位置。有什么建议吗?

3 个答案:

答案 0 :(得分:5)

w +应该在引号中。

File.new("/clean/_cleaned" + filename, "w+")

答案 1 :(得分:5)

您可以使用Fileutils.cp复制文件:

require 'fileutils'
Fileutils.cp source, dest

答案 2 :(得分:2)

require 'fileutils'
plays = %w[RomeoAndJuliet Hamlet TheTempest TheMerchantofVenice AMidSummerNightsDream]
plays.each do |play|
  Fileutils.cp "#{play}.txt", "/clean/_cleaned#{play}.txt"
end

或者,要复制目录中的所有.txt文件,而不是显式列表:

Dir['*.txt'].each do |file|
  Fileutils.cp "#{file}", "/clean/_cleaned#{file}"
end