最近在用Ftplib 模塊中的 dir方法 ,想用變量存儲一下 返回的目錄,發現返回的是None ,看了下源碼發現並沒有返回值, 只是會默認輸出到標准輸出流,百度了一下 也沒百度到解決方案。
#Ftplib模塊中 dir方法的定義 發現無返回值
def dir(self, *args):
'''List a directory in long form.
By default list current directory to stdout.
Optional last argument is callback function; all
non-empty arguments before it are concatenated to the
LIST command. (This *should* only be used for a pathname.)'''
cmd = 'LIST'
func = None
if args[-1:] and type(args[-1]) != type(''):
args, func = args[:-1], args[-1]
for arg in args:
if arg:
cmd = cmd + (' ' + arg)
self.retrlines(cmd, func)
研究了一下,發現直接在Ftplib源碼中新寫一個dirs方法就可以解決返回值的問題:
這里用了templist ,返回為所有目錄的列表
def dirs(self, *args):
'''List a directory in long form.
By default list current directory to stdout.
Optional last argument is callback function; all
non-empty arguments before it are concatenated to the
LIST command. (This *should* only be used for a pathname.)'''
cmd = 'LIST'
templist = []
func = None
if args[-1:] and type(args[-1]) != type(''):
args, func = args[:-1], args[-1]
for arg in args:
if arg:
cmd = cmd + (' ' + arg)
self.retrlines(cmd, templist.append)
return templist