使用python实现ftp的文件读写方法
作者:wljwlj 时间:2022-02-06 11:25:03
ftp登陆连接
from ftplib import FTP #加载ftp模块
ftp=FTP() #设置变量
ftp.set_debuglevel(2) #打开调试级别2,显示详细信息
ftp.connect("IP","port") #连接的ftp sever和端口
ftp.login("user","password") #连接的用户名,密码
print ftp.getwelcome() #打印出欢迎信息
ftp.cmd("xxx/xxx") #进入远程目录
bufsize=1024 #设置的缓冲区大小
filename="filename.txt" #需要下载的文件
file_handle=open(filename,"wb").write #以写模式在本地打开文件
ftp.retrbinaly("RETR filename.txt",file_handle,bufsize) #接收服务器上文件并写入本地文件
ftp.set_debuglevel(0) #关闭调试模式
ftp.quit() #退出ftp
ftp相关命令操作
ftp.cwd(pathname) #设置FTP当前操作的路径
ftp.dir() #显示目录下所有目录信息
ftp.nlst() #获取目录下的文件
ftp.mkd(pathname) #新建远程目录
ftp.pwd() #返回当前所在位置
ftp.rmd(dirname) #删除远程目录
ftp.delete(filename) #删除远程文件
ftp.rename(fromname, toname)#将fromname修改名称为toname。
ftp.storbinaly("STOR filename.txt",file_handel,bufsize) #上传目标文件
ftp.retrbinary("RETR filename.txt",file_handel,bufsize) #下载FTP文件
来源:https://blog.csdn.net/wljwlj/article/details/73332688
标签:python,ftp,文件,读写
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pandas数据筛选和csv操作的实现方法
2022-04-23 15:36:21
django xadmin中form_layout添加字段显示方式
2023-08-06 23:59:12
![](https://img.aspxhome.com/file/2023/0/76070_0s.jpg)
python通过线程实现定时器timer的方法
2023-04-28 17:49:28
python字符串运算符详情
2023-11-19 20:51:31
![](https://img.aspxhome.com/file/2023/9/64609_0s.png)
python中的opencv和PIL(pillow)转化操作
2023-04-01 18:54:46
python Django模板的使用方法
2021-06-09 15:29:14
![](https://img.aspxhome.com/file/2023/6/95336_0s.png)
Python中JsonPath提取器和正则提取器
2022-08-27 14:12:20
Python3读写Excel文件(使用xlrd,xlsxwriter,openpyxl3种方式读写实例与优劣)
2022-10-10 18:17:38
![](https://img.aspxhome.com/file/2023/9/103309_0s.png)
ffmpeg+Python实现B站MP4格式音频与视频的合并示例代码
2023-02-01 00:08:57
![](https://img.aspxhome.com/file/2023/7/65887_0s.png)
Oracle 子程序参数模式,IN,OUT,NOCOPY
2009-10-23 18:08:00
Python实现读取txt文件并画三维图简单代码示例
2023-06-11 15:44:18
wxPython学习之主框架实例
2023-11-10 00:41:56
![](https://img.aspxhome.com/file/2023/4/63554_0s.jpg)
Python 图片视频模糊化实现案例
2023-05-28 11:50:52
![](https://img.aspxhome.com/file/2023/4/72824_0s.jpg)
Django 如何实现文件上传下载
2021-07-16 02:54:15
![](https://img.aspxhome.com/file/2023/0/86270_0s.png)
Oracle 11g安装错误提示未找到wfmlrsvcapp.ear的解决方法
2023-07-14 14:31:52
![](https://img.aspxhome.com/file/2023/6/64466_0s.jpg)
详解Python如何实现惰性导入-lazy import
2023-12-22 20:15:43
arcgis使用Python脚本进行批量截图功能实现
2021-04-25 03:40:05
![](https://img.aspxhome.com/file/2023/7/95777_0s.jpg)
Python 爬虫学习笔记之单线程爬虫
2021-09-23 10:47:27
![](https://img.aspxhome.com/file/2023/1/110231_0s.png)
浅谈Python2.6和Python3.0中八进制数字表示的区别
2023-04-22 23:56:42
教你利用python如何读取txt中的数据
2023-04-03 14:52:36
![](https://img.aspxhome.com/file/2023/0/66420_0s.png)