Ruby Net::FTP gettextfile 无法在本地保存文件



我正在尝试从ftp站点检索文件(.csv)并将它们全部保存在本地同一文件夹中。我的代码如下所示:

#! /usr/bin/ruby
require 'logger'
require 'fileutils'
require 'net/ftp'
require 'rubygems'
require 'mysql2'
require 'roo'
require 'date'
# logging setup
log = Logger.new("/path_to_logs/ftp_log.log", 10, 1024000)
log.level = Logger::INFO
export_ftp_path = '/Receive/results/'
export_work_path ='/Users/pierce/results_exports/'
Net::FTP.open('host', 'username', 'password') do |ftp|
    log.info("Logged into FTP")
    ftp.passive = true
    ftp.chdir("#{export_ftp_path}")
      ftp.list.each do |file|
        log.info("Found file #{file}")
        new_file = file[56..115]  #take part of the file name and remove spaces and periods
        new_file = new_file.gsub(/[.]+/, "") 
        new_file = new_file.gsub(/s/, "0") 
        ftp.gettextfile(file,"#{new_file}")
        log.info("Downloaded file #{new_file}")
      end
end

这是我收到的错误:

/Users/pierce/.rbenv/versions/1.9.2-p290/lib/ruby/1.9.1/net/ftp.rb:560:in `initialize': No such file or directory -  (Errno::ENOENT)
    from /Users/pierce/.rbenv/versions/1.9.2-p290/lib/ruby/1.9.1/net/ftp.rb:560:in `open'
    from /Users/pierce/.rbenv/versions/1.9.2-p290/lib/ruby/1.9.1/net/ftp.rb:560:in `gettextfile'
    from ftp_test.rb:44:in `block (2 levels) in <main>'
    from ftp_test.rb:33:in `each'
    from ftp_test.rb:33:in `block in <main>'
    from /Users/pierce/.rbenv/versions/1.9.2-p290/lib/ruby/1.9.1/net/ftp.rb:116:in `open'

正如建议的那样,这是我对放置文件和放置new_file的值。

file = -rwxr-xr-x    1 1130419  114727       9546 May 17 08:11 results_Wed. 16 May 2012.csv
new_file = results_Wed0230May02012csv

关于在gettextfile或我的脚本中更改什么以使文件正确保存的任何建议?

当你只需要目录中的文件列表时,你应该使用nlst而不是list。否则,需要正确解析list的输出。

当您请求文件时,它必须是原始文件名,包括所有空格。当您保存文件时,它可以是您想要的任何内容(包括空格或不包括空格)。该错误是因为您请求了错误的文件。请改用nlst。这将使它更容易(无需转换或解析)。

相关内容

  • 没有找到相关文章

最新更新