problem with using Tempfile 
Author Message
 problem with using Tempfile

I've written a small script which removes ctrl-M's and an ending
ctrl-Z from files which I'm including below. When the lines

    #ftmp.open()
    #ftmp.close(true)

are uncommented, everything works fine. When they're commented out, I
get errors like the following:

processing act_func.cpp
processing activity.cpp
processing anneal.cpp
processing confuse.cpp
processing conjgrad.cpp
processing control.cpp
processing direcmin.cpp
processing dotprod.cpp
processing execute.cpp
processing gen_init.cpp
/home/wiener/ruby/rmctlm.rb:29:in `process': failed to convert nil
into Array (TypeError)
        from /home/wiener/ruby/rmctlm.rb:24:in `each'
        from /home/wiener/ruby/rmctlm.rb:24:in `process'
        from /home/wiener/ruby/rmctlm.rb:15:in `each'
        from /home/wiener/ruby/rmctlm.rb:15:in `process'
        from /home/wiener/ruby/rmctlm.rb:59

Does anyone have an idea what's going wrong here?

Thanks,

Gerry Wiener

-------------------------------------------------------------------------------
#!/usr/bin/env ruby

require "tempfile"

# Remove control M's from file

def usage()
  printf("usage: %s files\n", $0)
  exit(2)
end

def process(files)

  for f in files
    printf("processing %s\n", f)
    change = false

    fp = File.new(f, "r")
    lines = fp.readlines()
    fp.close()

    ftmp = Tempfile.new(File.basename(f))
    for l in lines
      if l[-2] == ?\C-m
        # Remove control-M
        outl = l[0...-2] + "\n"

        ftmp.write(outl)
        change = true
      else
        if l[0] == ?\C-z
          # Don't write out a line containing control Z
          change = true
        else
          ftmp.write(l)
        end
      end
    end

    ftmp.close()

    if change
      s = sprintf("cp %s %s", ftmp.path(), f)
      system(s)
    end

    #ftmp.open()
    #ftmp.close(true)

  end
end

if (ARGV.length() == 0)
  usage()
end

process(ARGV)



Tue, 27 Jan 2004 07:06:56 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. tempfile.mktemp no longer obeys tempfile.tempdir?

2. using tempfile policie

3. tempfile problem CosmoPlayer2.1 & NSC4.5

4. tempfile problem on win98

5. Tempfile.py destructor problem (python 1.4)

6. Tempfile warning message: "output left in <tempfile>"?

7. PStore and tempfiles - bug?

8. Tempfile

9. Possible bug in Tempfile/Fork interaction

10. tempfile.tempdir

11. tempfile bug or doc bug

12. tempfile creation

 

 
Powered by phpBB® Forum Software