即使目录确实存在于列表中,代码也会卡住。我不确定这里发生了什么。当我注释掉我编写的代码时,如果目录不存在则重新询问用户目录,代码完美无缺。
def path_sel():
path = raw_input("Select desired working directory: ")
b = []
ftp.retrlines('LIST', b.append)
if path not in b:
print "ERROR- Directory does not exist.\n"
path_sel()
else:
print '\nChanging to '+ path, '\n'
ftp.cwd(path)
print path
ftp.retrlines('LIST')
c = []
ftp.retrlines('LIST', c.append)
if 'd' in str(c[0]):
path_sel()
file_dl()
答案 0 :(得分:0)
这似乎对我有用(我选择了一个随机的FTP服务器):
from ftplib import FTP
def path_sel():
path = raw_input("Select desired working directory: ")
b = []
ftp.retrlines('LIST', b.append)
found = False
for entry in b:
if path in entry:
found = True
if not found:
print "ERROR- Directory does not exist.\n"
path_sel()
else:
print '\nChanging to '+ path, '\n'
ftp.cwd(path)
print path
ftp.retrlines('LIST')
# After printing the directory, you probably
# want to ask the user to enter a new file/path
# rather than just assuming that they are interested
# in the first entry in the ftp.retrlines() printout
c = []
ftp.retrlines('LIST', c.append)
if 'd' in str(c[0]):
path_sel()
# ---------------------------------------------------
# Once you've extracted a filename, run your function
# ---------------------------------------------------
print 'Running file_dl()'
#file_dl(filename)
ftp = FTP('ftp.debian.org')
ftp.login()
path_sel()
它会不断返回“ERROR-目录不存在”,并在输入有效路径之前请求新输入。一旦你找到一个文件(不是以'drwxrwxrwx ....'开头),你想要执行你的file_dl()函数。
以下是我的控制台中的内容: