let\'s say we have two functions:
def ftpConnect():
ftp = FTP(\'server\')
ftp.login()
ftp.cwd(\'/path\')
def getFileList():
ftpConnect()
fi
In my opinion, the most elegant solution would be to make a FTP-class, which would have the ftp
-variable as a private attribute.
class FTPConnection(object):
def __init__(self, server):
self._ftp = FTP(server)
def connect(self):
self._ftp.login()
self._ftp.cwd('/path')
def getFileList():
files = self._ftp.nlst()
print(files)
ftp = FTPConnection('server')
ftp.connect()
ftp.getFileList()