简体   繁体   English

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

[英]Ruby Net::FTP gettextfile not able to save files locally

I am trying to retrieve files (.csv) from an ftp site and save them all locally in the same folder. 我正在尝试从ftp站点检索文件(.csv),并将它们全部本地保存在同一文件夹中。 My code looks like this: 我的代码如下所示:

#! /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

And here is the error I receive: 这是我收到的错误:

/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'

As suggested, here are the values I have for puts file and puts new_file. 如建议的那样,这是我对于puts文件和puts new_file的值。

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

Any suggestions on what to change in gettextfile or within my script to get the files saved correctly? 关于在gettextfile或在脚本中进行哪些更改以正确保存文件的任何建议?

You should use nlst instead of list when you just need a list of files in a directory. 当只需要目录中的文件list时,应使用nlst而不是list The output of list needs to be properly parsed otherwise. 否则, list的输出需要正确解析。

When you request the file it has to be the original filename, including all spaces. 当您请求文件时,它必须是原始文件名,包括所有空格。 When you save the file it can be anything you want (including spaces or not). 保存文件时,它可以是您想要的任何内容(包括空格)。 The error was because you were requesting the wrong file. 该错误是因为您请求的文件错误。 Use nlst in your case instead. 在您的情况下,请使用nlst It will make it much easier (no conversion or parsing needed). 这将使它变得更加容易(无需转换或解析)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM