2016-04-13 9 views
0

一時ファイルからファイルに書き込んでいますが、一時ファイルから書き込まれたファイルを読み込もうとすると、ディレクトリに余分な文字が追加されているようですtmp 。 (ファイルはoptparseの経由で渡される)一時ファイルから書き換え中にエラーが発生しました

出典:

require 'tempfile' 

PATH = Dir.pwd 

def format_file 
    puts 'Writing to temporary file..' 
    if File.exists?(OPTIONS[:file]) 
    file = Tempfile.new('file') 
    IO.read(OPTIONS[:file]).each_line do |s| 
     File.open(file, 'a+') { |format| format.puts(s) unless s.chomp.empty? } 
    end 
    IO.read(file).each_line do |file| 
     File.open("#{PATH}/tmp/#sites.txt", 'a+') { |line| line.puts(file) } 
    end 
    puts "File: #{OPTIONS[:file]}, has been formatted and saved as #sites.txt in the tmp directory." 
    else 
    puts <<-_END_ 
         Woah now my friend! I know you're eager to get those vulns; 

         But file: #{OPTIONS[:file]} doesn't exist or in this directory at least! 

         What I'm gonna need you to do is go move that file over here. 
         It's okay, you're forgiven, I'll wait until you return.. 
    _END_ 

    end 
end 

例:それは何

ruby whitewidow.rb -f sites.txt 

[12:40:43 INFO]Formatting file 
[12:40:43 INFO]Writing to temporary file.. 
[12:40:43 INFO]File: tmp/sites.txt, has been formatted and saved as #sites.txt in the tmp directory. 
[12:40:43 INFO]Let's check out this file real quick like.. 
whitewidow.rb:224:in `read': No such file or directory @ rb_sysopen - C:/Users/Justin/MyScripts/RubySQL/whitewidow/#tmp/#sites.txt (Errno::ENOENT) 
#<= Correct path but the '#' in tmp shouldn't be there.. 

が、その中に任意の空行を削除するには、ファイル形式です(このプログラムにはありません空白行のように)一時ファイルに書き込んで、元のディレクトリ(whitewidow/tmp /)に一時ファイルを書き戻し、一時ファイルを削除する必要があります(私はこの部分を行う方法を知っています)。

オリジナルのディレクトリに書き直しているうちに、ディレクトリ名に#を追加しているようですが(#tmpは実際にはtmpです)、これを追加している理由はありますか?

答えて

0

何らかの理由で、プログラムが#をパスに追加していたので、私はを編集しました。それは#です。

関連する問題