如何让Python检查ftp目录是否存在? [英] How to make Python check if ftp directory exists?

查看:2642
本文介绍了如何让Python检查ftp目录是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个脚本连接到示例ftp服务器并列出可用的目录:

I'm using this script to connect to sample ftp server and list available directories:

from ftplib import FTP
ftp = FTP('ftp.cwi.nl')   # connect to host, default port (some example server, i'll use other one)
ftp.login()               # user anonymous, passwd anonymous@
ftp.retrlines('LIST')     # list directory contents
ftp.quit()

如何使用ftp.retrlines('LIST')输出来检查目录(例如public_html)是否存在,是否存在cd,然后执行其他代码并退出;如果不立即执行代码并退出?

How do I use ftp.retrlines('LIST') output to check if directory (for example public_html) exists, if it exists cd to it and then execute some other code and exit; if not execute code right away and exit?

推荐答案

Nslt将列出ftp服务器中所有文件的数组。

Nslt will list an array for all files in ftp server. Just check if your folder name is there.

from ftplib import FTP 
ftp = FTP('yourserver')
ftp.login('username', 'password')

folderName = 'yourFolderName'
if folderName in ftp.nlst():
    #do needed task 

这篇关于如何让Python检查ftp目录是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆