编写一个小脚本来复制一些文件,但是我收到错误:
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
所有括号似乎都应该是它们的位置。有什么建议吗?
答案 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