Output file name based on original file name

I have a simple txt parsing script that I’d like to have the output be
named after the original text file.

original = original.txt
output = original_output.txt

I’m just doing a simple file.open, and couldn’t find anything.

File.open(‘output.txt’, ‘w’) do |f2|
File.readlines(“original.txt”).each do |line|

Thanks in advance for any help.

Collin M. wrote:

Thanks in advance for any help.

This is the simplest…
output_file = “#{input_file}_output.txt”

On the other hand, you can look at the FileUtils to see how to get the
base name or the name without extension, etc… so that you can make it
more productive. I’m just suggesting the simplest.

Cheers,
Mohit.
10/16/2009 | 1:53 AM.

Hi,

Am Freitag, 16. Okt 2009, 02:44:38 +0900 schrieb Collin M.:

I’m just doing a simple file.open, and couldn’t find anything.

File.open(‘output.txt’, ‘w’) do |f2|
File.readlines(“original.txt”).each do |line|

Do you get the below error message?

irb(main):001:0> File.exists? “nonexistent”
=> false
irb(main):002:0> File.read “nonexistent”
Errno::ENOENT: No such file or directory - nonexistent
from (irb):2:in `read’
from (irb):2
irb(main):003:0>

Bertram

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs