Newbie - open file permission denied

Discussion in 'Ruby' started by Bob Smith, Dec 24, 2008.

  1. Bob Smith

    Bob Smith Guest

    I am attempting to open a directory, open each file and send each line
    of the file to a network host.
    I can get the code to work for a single file, but the script fails with
    a "permission denied" error when I attempt to do this for each file in a
    directory.

    *****CODE*****
    IPADDR= "some ip address"
    OUTPORT = "some ip port"
    INDIR = "some directory"
    dir = Dir.open(INDIR)
    dir.each do |f|
    file = File.open(f,"r")
    streamSock = TCPSocket::new(IPADDR,OUTPORT)
    while(line = file.gets)
    streamSock.send(line,0)
    end
    streamSock.close
    file.close
    end
    *****END CODE******
    any help greatly appreciated

    Thank you.
    --
    Posted via http://www.ruby-forum.com/.
    Bob Smith, Dec 24, 2008
    #1
    1. Advertising

  2. Bob Smith

    Tim Hunter Guest

    Bob Smith wrote:
    > I am attempting to open a directory, open each file and send each line
    > of the file to a network host.
    > I can get the code to work for a single file, but the script fails with
    > a "permission denied" error when I attempt to do this for each file in a
    > directory.
    >
    > *****CODE*****
    > IPADDR= "some ip address"
    > OUTPORT = "some ip port"
    > INDIR = "some directory"
    > dir = Dir.open(INDIR)
    > dir.each do |f|
    > file = File.open(f,"r")
    > streamSock = TCPSocket::new(IPADDR,OUTPORT)
    > while(line = file.gets)
    > streamSock.send(line,0)
    > end
    > streamSock.close
    > file.close
    > end
    > *****END CODE******
    > any help greatly appreciated
    >
    > Thank you.


    The filenames returned by dir.each are just filenames, not the complete
    paths. You may need to prepend the INDIR directory name to the filenames
    to get a complete path to open.


    --
    RMagick: http://rmagick.rubyforge.org/
    Tim Hunter, Dec 24, 2008
    #2
    1. Advertising

  3. Bob Smith

    Guest

    On Wed, Dec 24, 2008 at 10:53 AM, Tim Hunter <> wrote:
    > Bob Smith wrote:
    >>
    >> I am attempting to open a directory, open each file and send each line
    >> of the file to a network host...
    >>
    >> dir = Dir.open(INDIR)
    >> dir.each do |f|
    >> file = File.open(f,"r")

    >
    > The filenames returned by dir.each are just filenames, not the complete
    > paths. You may need to prepend the INDIR directory name to the filenames to
    > get a complete path to open.


    Another possible option might be something like:

    Dir.glob("#{INDIR}/*").each do |filename|
    File.open(filename, "r") do |file|
    #...
    end
    end

    glob does return the full path to the file.

    Or, possibly something like this could work:

    Dir.chdir(INDIR) do
    Dir.foreach(".") do |filename|
    #...
    end
    end
    , Dec 24, 2008
    #3
  4. Bob Smith

    Bob Smith Guest

    unknown wrote:
    >
    > Dir.glob("#{INDIR}/*").each do |filename|
    > File.open(filename, "r") do |file|
    > #...
    > end
    > end
    >
    > glob does return the full path to the file.


    Thank you, the Dir.glob gets rid of the error.
    --
    Posted via http://www.ruby-forum.com/.
    Bob Smith, Dec 24, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer
    Replies:
    3
    Views:
    2,989
    Kevin Spencer
    Aug 15, 2003
  2. gmax2006
    Replies:
    3
    Views:
    17,124
    Damjan
    Aug 9, 2006
  3. Bill Scherer
    Replies:
    0
    Views:
    2,065
    Bill Scherer
    Jul 20, 2006
  4. fniles

    HTTP.Open - Permission Denied error

    fniles, Apr 10, 2006, in forum: ASP General
    Replies:
    3
    Views:
    228
    Anthony Jones
    Apr 11, 2006
  5. Replies:
    5
    Views:
    144
Loading...

Share This Page