python-2.7 - 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:无效参数
此外,文件的大小因方法而异:
FileZilla : 22 972 Ko
Python ftplib:23 005 Ko
写入检索到的文件时是 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)
解决方案
实际上,这是二进制配置的问题(感谢您的提问)。
我ftp.voidcmd('TYPE I')
在使用 ftplib 检索文件之前添加,然后我修改了本地文件的写入参数new_file = open('%s/%s' % (local_ftp_path, filename), "wb")
以指定这是一个二进制文件。
现在该文件在通过 ftplib 下载后可以读取,并且与从 FileZilla 下载的大小相同。
感谢您的贡献。
推荐阅读
- javascript - Flask:无法连接到虚拟服务器上的 REDIS
- c++ - 随机整数范围 0 到 100,结果总和也不能超过 100
- python - 从在线集群中的视频中提取帧的代码有什么问题?
- django - 如何将参数日期传递给 url 调度程序但作为命名参数
- sql - 您可以结合 case 和 top 语句来标记哪些值在前几个(Microsoft SQL)
- python - 格式化多维数组
- javascript - 有没有办法在导出对象的同时保留 JavaScript 中的变量?
- amazon-web-services - AWS 目标组运行状况不佳
- r - x 轴作为周数,辅助 x 轴作为日期
- html - 如果 Thymeleaf 中的条件。验证日期是 DayOfWeek。使用 th:text 它打印 MONDAY 但如果我进行检查它不起作用