开发者

Ruby Syntax Error: File Copying

Coding a small script to copy some files, however I get the error:

fileops.rb:6: syntax error, unexpected ')'

Heres my code

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

All the brackets appear to be where they should be. Any suggestions?


w+ should be in quotes.

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


You can use Fileutils.cp to copy a file:

require 'fileutils'
Fileutils.cp source, dest


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

Or, to copy all .txt files in the directory, instead of an explicit list:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜