python 判断ftp是否存在目录,若不存在则ftp远程创建一个或者层级目录

时间: 2023-08-02 21:20:48 浏览: 56
可以使用 Python 的 ftplib 库来实现判断是否存在目录,并在不存在目录时创建一个或者多个层级目录的功能。具体步骤如下: 1. 连接 FTP 服务器: ```python from ftplib import FTP ftp = FTP(host, user, password) ``` 2. 判断目录是否存在,可以使用 `nlst` 方法获取当前目录下的所有文件和目录列表,并判断需要的目录是否存在: ```python def is_ftp_dir_exists(ftp, remote_path): file_list = [] try: ftp.retrlines('LIST ' + remote_path, file_list.append) except Exception as e: print(e) return False for file in file_list: if file.split()[-1] == remote_path.split('/')[-1]: return True return False ``` 3. 如果目录不存在,则使用 `mkd` 方法逐层创建目录: ```python def ftp_mkdir_recursive(ftp, remote_path): if is_ftp_dir_exists(ftp, remote_path): return True else: parent_dir = remote_path[:remote_path.rfind('/')] if ftp_mkdir_recursive(ftp, parent_dir): try: ftp.mkd(remote_path) return True except Exception as e: print(e) return False else: return False ``` 完整代码示例: ```python from ftplib import FTP host = 'ftp.example.com' user = 'username' password = 'password' remote_path = '/remote/path/to/directory' ftp = FTP(host, user, password) def is_ftp_dir_exists(ftp, remote_path): file_list = [] try: ftp.retrlines('LIST ' + remote_path, file_list.append) except Exception as e: print(e) return False for file in file_list: if file.split()[-1] == remote_path.split('/')[-1]: return True return False def ftp_mkdir_recursive(ftp, remote_path): if is_ftp_dir_exists(ftp, remote_path): return True else: parent_dir = remote_path[:remote_path.rfind('/')] if ftp_mkdir_recursive(ftp, parent_dir): try: ftp.mkd(remote_path) return True except Exception as e: print(e) return False else: return False if ftp_mkdir_recursive(ftp, remote_path): print('目录创建成功!') else: print('目录创建失败!') ftp.quit() ``` 注意:FTP 服务器需要有创建目录的权限。

相关推荐

最新推荐

recommend-type

Python判断是否json是否包含一个key的方法

今天小编就为大家分享一篇Python判断是否json是否包含一个key的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

python执行scp命令拷贝文件及文件夹到远程主机的目录方法

今天小编就为大家分享一篇python执行scp命令拷贝文件及文件夹到远程主机的目录方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

python判断文件夹内是否存在指定后缀文件的实例

今天小编就为大家分享一篇python判断文件夹内是否存在指定后缀文件的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

在Python 不同级目录之间模块的调用方法

今天小编就为大家分享一篇在Python 不同级目录之间模块的调用方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

python读取目录下所有的jpg文件,并显示第一张图片的示例

今天小编就为大家分享一篇python读取目录下所有的jpg文件,并显示第一张图片的示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。