Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6944969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:22:32+00:00 2026-05-27T13:22:32+00:00

I’m trying to write a utility function that will open three different types of

  • 0

I’m trying to write a utility function that will open three different types of files: .bz2, .gz, and .txt. I can’t just use File.read because it gives me garbage back for the compressed files. I’m trying to use Open3.popen3 so that I can give it a different command, but I’m getting a ‘no such file or directory’ error with the following code:

def file_info(file)
  cmd = ''
  if file.match("bz2") then
    cmd = "bzcat #{file}"# | head -20"
  elsif file.match("gz") then
    cmd = "gunzip -c #{file}"
  else
    cmd = "cat #{file}"
  end

  puts "opening file #{file}"
  Open3.popen3("#{cmd}", "r+") { |stdin, stdout, stderr|
    puts "stdin #{stdin.inspect}"
    stdin.read {|line|
      puts "line is #{line}"
      if line.match('^#') then
      else
        break
      end
    }
  }
end


> No such file or directory - cat /tmp/test.txt

The file does exist. I’ve tried using cmd instead of #{cmd} with the same results in the popen3 cmd.

I decided to hardcode it to do the txt file as follows:

def file_info(file)
  puts "opening file #{file}"
  Open3.popen3("cat", file, "r+") { |stdin, stdout, stderr|
    puts "stdin #{stdin.inspect}"
    stdin.read {|line|
      puts "line is #{line}"
      if line.match('^#') then
      else
        break
      end
    }
  }
end

This gives me back:

stdin #<IO:fd 6>
not opened for reading

What am I doing wrong?

When I do:

Open3.popen3("cat",file) { |stdin, stdout, stderr|
  puts "stdout is #{stdout.inspect}"
  stdout.read {|line|
    puts "line is #{line}"
    if line.match('^#') then
      puts "found line #{line}"
    else
      break
    end
  }
}

I get no errors and the STDOUT line is printed, but neither line statement prints out anything.

After trying several different things, the solution I came up with was:

cmd = Array.new
if file.match(/\.bz2\z/) then
  cmd = [ 'bzcat', file ]
elsif file.match(/\.gz\z/) then
  cmd = [ 'gunzip', '-c', file ]
else
  cmd = [ 'cat', file ]
end

Open3.popen3(*cmd) do |stdin, stdout, stderr|
  puts "stdout is #{stdout}"
  stdout.each do |line|
    if line.match('^#') then
      puts "line is #{line}"
    else
      break
    end
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:22:33+00:00Added an answer on May 27, 2026 at 1:22 pm

    From the fine manual (which is rather confusingly written):

    *popen3(cmd, &block)
    […]
    So a commandline string and list of argument strings can be accepted as follows.

    Open3.popen3("echo a") {|i, o, e, t| ... }
    Open3.popen3("echo", "a") {|i, o, e, t| ... }
    Open3.popen3(["echo", "argv0"], "a") {|i, o, e, t| ... }
    

    So when you do this:

    Open3.popen3("cat /tmp/test.txt", "r+")
    

    popen3 thinks that the command name is cat /tmp/test.txt and r+ is an argument to that command, hence the specific error that you’re seeing:

    No such file or directory – cat /tmp/test.txt

    There’s no need for the usual mode flags ("r+") with Open3.popen3 since it will separate handles for reading, writing, and errors; and, as you’ve seen, trying to supply the mode string just causes bugs and confusion.

    The second case:

    Open3.popen3("cat", file, "r+") { |stdin, stdout, stderr|
      stdin.each {|line|
        #...
    

    Doesn’t work because stdin is the command’s standard input and that’s what you would write to not read from, you’d want to stdout.read instead.

    You should be building your commands as arrays and your match calls should be a little stricter:

    if file.match(/\.bz2\z/) then
      cmd = [ 'bzcat', file ]
    elsif file.match(/\.gz\z/) then
      cmd = [ 'gunzip', '-c', file ]
    else
      cmd = [ 'cat', file ]
    end
    

    and then splat them:

    Open3.popen3(*cmd) do |stdin, stdout, stderr|
      #...
    end
    

    Not only does this work but it will save you from funny filenames.

    You could also avoid a useless use of cat (which someone will probably complain about) by skipping the Open3.popen3 for the non-compressed cases and using File.open instead. You might also want to consider checking the file’s bytes to see what it contains rather than relying on the extension (or use ruby-filemagic to check for you).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.