我在想是否与从模块中导入我需要的东西相关,以减少任何脚本的内存消耗,或者我应该只导入所有内容?我相信如果我开始这样写可能会消耗更多的时间,但值得吗?我的意思是,可以减少更多使用内存的机会吗?
使用下面的代码,至少相关部分是我打算做什么的当前示例:
from ftplib import FTP as FTP_LIB
from ftplib.FTP import connect as FTP_CONNECT
from ftplib.FTP import login AS FTP_LOGIN
from ftplib.FTP import cwd as FTP_CWD
from ftplib.FTP import storbinary as FTP_STORE_BIN
from ftplib.FTP import retrbinary as FTP_RETRIV_BIN
from ftplib.FTP import delete as FTP_DELETE
from ftplib.FTP import quit as FTP_QUIT
from zipfile import ZipFile
from zipfile import ZIP_DEFLATED
from sys import exit as SYS_EXIT
#--------------------------------------------------------------------------
# FTP Download
#--------------------------------------------------------------------------
def get_file(self, iServer, ftpPort, login, pwd, fileName, path):
parts = iServer.split(":")
host = parts[0]
ftp = FTP_LIB()
try:
FTP_CONNECT(host, ftpPort, 20)
FTP_LOGIN(login, pwd)
FTP_CWD(path)
FTP_RETRIV_BIN('RETR ' + fileName, open(fileName, 'wb').write)
except Exception, e:
print " Download failed : " + str(e)
SYS_EXIT(1)
finally:
FTP_QUIT()
提前致谢。