/ Published in: Ruby
Basic example of how to open a text file and print them (including line-numbers) into a new text file
Expand |
Embed | Plain Text
counter = 1 old_file = File.open("./Car.txt", "r") new_file = File.new("./NewCar.txt", "w") while( line = old_file.gets ) new_file.puts "#{counter}: #{line}" counter = counter + 1 end new_file.close
Comments
Subscribe to comments
You need to login to post a comment.

dude.. why not:
File.open("./lol.txt", "r") { |file| File.new("./#{ File.basename(file.path, ".txt") }_new.txt", "w+").puts(file.read) }
Or:
File.open("./lol.txt", "r") { |fileold| filenewfilename = "#{ File.basename(file.path, ".txt") }new.txt" #append "new" to the end of the filename filenew = File.new(filenewfilename, "w+") file_new.puts(file.read) }
Gah, the site messed up the second one. but you get the point. Ruby is stronger than you think
Right on "c00lryguy", thanks for the post. You're right too, Ruby is one bad ass language.