首页 > 解决方案 > Python 2.7:使用 ftplib 检索 netCDF4 文件

问题描述

这是我的问题:我从 FTP 服务器下载了一些 netCDF4 文件,有两种不同的方式:通过 FileZilla 和使用 ftplib 的 Python 2.7 脚本。

Python 脚本代码(在 Windows 上运行):

# download the file
try:
    ftp = FTP(server_address)
    ftp.login(server_login, server_pass)
    filepath = 'the_remote_rep/myNetCDF4File.nc'
    filename = 'myNetCDF4File.nc'
    local_dir = 'toto'
    new_file = open('%s/%s' % (local_dir, filename), "w")
    ftp.retrbinary('RETR %s' % filepath, new_file.write)
    ftp.close()
    new_file.close()
except Exception as e:
    print("Error FTP : '" + str(e) + "'")

# update title into the file
try:
    fname = 'toto/myNetCDF4File.nc'
    dataset = netCDF4.Dataset(fname, mode='a')
    setattr(dataset, 'title', 'In Situ Observation Re-Analysis')
    dataset.close()
except Exception as e:
    print("Error netCDF4 : '" + str(e) + "'")

然后,我收到这条消息:

错误 netCDF4:'[Errno 22] 无效参数:'toto/myNetCDF4File.nc''

当我尝试使用通过 FileZilla 下载的 netCDF4 文件(例如同一个文件)的第二个代码块时,没有错误。此外,当我尝试使用“ncdump -k”获取文件的 netCDF 版本时,以下是响应(与其他文件一致):

ncdump:myNetCDF4File.nc:无效参数

此外,文件的大小因方法而异:

写入检索到的文件时是 ftplib 的问题吗?还是我错过了一些参数来正确编码文件?

提前致谢。

编辑:来自 FileZilla 的详细消息:

...
Response:   230 Login successful.
Trace:  CFtpLogonOpData::ParseResponse() in state 5
Trace:  CControlSocket::SendNextCommand()
Trace:  CFtpLogonOpData::Send() in state 9
Command:    OPTS UTF8 ON
Trace:  CFtpControlSocket::OnReceive()
Response:   200 Always in UTF8 mode.
Trace:  CFtpLogonOpData::ParseResponse() in state 9
Status: Logged in
Trace:  Measured latency of 114 ms
Trace:  CFtpControlSocket::ResetOperation(0)
Trace:  CControlSocket::ResetOperation(0)
Trace:  CFtpLogonOpData::Reset(0) in state 14
Trace:  CFtpControlSocket::FileTransfer()
Trace:  CControlSocket::SendNextCommand()
Trace:  CFtpFileTransferOpData::Send() in state 0
Status: Starting download of /INSITU_OBSERVATIONS/myNetCDF4File.nc
Trace:  CFtpChangeDirOpData::Send() in state 0
Trace:  CFtpControlSocket::ResetOperation(0)
Trace:  CControlSocket::ResetOperation(0)
Trace:  CFtpChangeDirOpData::Reset(0) in state 0
Trace:  CFtpFileTransferOpData::SubcommandResult(0) in state 1
Trace:  CControlSocket::SendNextCommand()
Trace:  CFtpFileTransferOpData::Send() in state 5
Trace:  CFtpRawTransferOpData::Send() in state 2
Command:    PASV
Trace:  CFtpControlSocket::OnReceive()
Response:   227 Entering Passive Mode (193,68,190,45,179,16).
Trace:  CFtpRawTransferOpData::ParseResponse() in state 2
Trace:  CControlSocket::SendNextCommand()
Trace:  CFtpRawTransferOpData::Send() in state 4
Trace:  Binding data connection source IP to control connection source IP 134.xx.xx.xx
Command:    RETR myNetCDF4File.nc
Trace:  CTransferSocket::OnConnect
Trace:  CFtpControlSocket::OnReceive()
Response:   150 Opening BINARY mode data connection for myNetCDF4File.nc (9411620 bytes).
Trace:  CFtpRawTransferOpData::ParseResponse() in state 4
Trace:  CControlSocket::SendNextCommand()
Trace:  CFtpRawTransferOpData::Send() in state 5
Trace:  CTransferSocket::TransferEnd(1)
Trace:  CFtpControlSocket::TransferEnd()
Trace:  CFtpControlSocket::OnReceive()
Response:   226 Transfer complete.
Trace:  CFtpRawTransferOpData::ParseResponse() in state 7
Trace:  CFtpControlSocket::ResetOperation(0)
Trace:  CControlSocket::ResetOperation(0)
Trace:  CFtpRawTransferOpData::Reset(0) in state 7
Trace:  CFtpFileTransferOpData::SubcommandResult(0) in state 7
Trace:  CFtpControlSocket::ResetOperation(0)
Trace:  CControlSocket::ResetOperation(0)
Trace:  CFtpFileTransferOpData::Reset(0) in state 7
Status: File transfer successful, transferred 9 411 620 bytes in 89 seconds
Status: Disconnected from server
Trace:  CFtpControlSocket::ResetOperation(66)
Trace:  CControlSocket::ResetOperation(66)

标签: python-2.7ftpnetcdfftplibnetcdf4

解决方案


实际上,这是二进制配置的问题(感谢您的提问)。

ftp.voidcmd('TYPE I')在使用 ftplib 检索文件之前添加,然后我修改了本地文件的写入参数new_file = open('%s/%s' % (local_ftp_path, filename), "wb")以指定这是一个二进制文件。

现在该文件在通过 ftplib 下载后可以读取,并且与从 FileZilla 下载的大小相同。

感谢您的贡献。


推荐阅读