python3 ftplib:通过 mlsd() 检查目录是否存在
python3 ftplib: check if dir exists via mlsd()
我正在使用 Python3 和 ftplib。
我需要检查 ftp 服务器上的目录是否存在。 (如果没有,我可以创建它,而不是cwd进去,如果它已经存在我会直接cwd进去)。
我在这里看到了 Marek Marecki 的方法:How to make Python check if ftp directory exists?
if 'foo' in [name for name, data in list(remote.mlsd())]:
问题是这也会在名为 'foo'.
的文件上触发
是否有 Pythonic 方式来执行此操作(明确地使用 mlsd())? nlst() 已弃用
非常感谢!
我没有任何支持 MLSD 命令的服务器,所以我不确定如何使用 mlsd 执行此操作。
但是即使没有 MLSD 支持,这段代码也应该可以工作。
items = []
ftp.retrlines('LIST', items.append )
items = map( str.split, items )
directorys = [ item.pop() for item in items if item[0][0] == 'd' ]
print( "directrys", directorys )
print( 'foo' in directorys )
也感谢@arnial
我想到了以下内容:(mlsd 或 nslt)
use_mlsd = 1
if(use_mlsd):
# if ftp server supports mlsd, use it, nlst is maked as deprecated in ftplib
# check if remotefoldername exists
remotefoldername_exists = 0
for name, facts in f.mlsd(".",["type"]):
if facts["type"] == "dir" and name == "remote_ftp":
print("isdir: "+ name)
remotefoldername_exists = 1
break
if(remotefoldername_exists == 0)
ftp.mkd(remotefoldername)
logging.debug("folder does not exitst, ftp.mkd: " + remotefoldername)
else:
logging.debug("folder did exist: " + remotefoldername)
else:
# nlst legacy support for ftp servers that do not support mlsd e.g. vsftp
items = []
ftp.retrlines('LIST', items.append )
items = map( str.split, items )
dirlist = [ item.pop() for item in items if item[0][0] == 'd' ]
#print( "directrys", directorys )
#print( 'remote_ftp' in directorys )
if not (remotefoldername in dirlist):
ftp.mkd(remotefoldername)
logging.debug("folder does not exitst, ftp.mkd: " + remotefoldername)
else:
logging.debug("folder did exist: " + remotefoldername)
我正在使用 Python3 和 ftplib。
我需要检查 ftp 服务器上的目录是否存在。 (如果没有,我可以创建它,而不是cwd进去,如果它已经存在我会直接cwd进去)。
我在这里看到了 Marek Marecki 的方法:How to make Python check if ftp directory exists?
if 'foo' in [name for name, data in list(remote.mlsd())]:
问题是这也会在名为 'foo'.
的文件上触发是否有 Pythonic 方式来执行此操作(明确地使用 mlsd())? nlst() 已弃用
非常感谢!
我没有任何支持 MLSD 命令的服务器,所以我不确定如何使用 mlsd 执行此操作。 但是即使没有 MLSD 支持,这段代码也应该可以工作。
items = []
ftp.retrlines('LIST', items.append )
items = map( str.split, items )
directorys = [ item.pop() for item in items if item[0][0] == 'd' ]
print( "directrys", directorys )
print( 'foo' in directorys )
也感谢@arnial
我想到了以下内容:(mlsd 或 nslt)
use_mlsd = 1
if(use_mlsd):
# if ftp server supports mlsd, use it, nlst is maked as deprecated in ftplib
# check if remotefoldername exists
remotefoldername_exists = 0
for name, facts in f.mlsd(".",["type"]):
if facts["type"] == "dir" and name == "remote_ftp":
print("isdir: "+ name)
remotefoldername_exists = 1
break
if(remotefoldername_exists == 0)
ftp.mkd(remotefoldername)
logging.debug("folder does not exitst, ftp.mkd: " + remotefoldername)
else:
logging.debug("folder did exist: " + remotefoldername)
else:
# nlst legacy support for ftp servers that do not support mlsd e.g. vsftp
items = []
ftp.retrlines('LIST', items.append )
items = map( str.split, items )
dirlist = [ item.pop() for item in items if item[0][0] == 'd' ]
#print( "directrys", directorys )
#print( 'remote_ftp' in directorys )
if not (remotefoldername in dirlist):
ftp.mkd(remotefoldername)
logging.debug("folder does not exitst, ftp.mkd: " + remotefoldername)
else:
logging.debug("folder did exist: " + remotefoldername)