Python:从FTP服务器下载文件

Python:从FTP服务器下载文件,第1张

Python:从FTP服务器下载文件

requests
库不支持ftp链接。

要从FTP服务器下载文件,您可以:

import urlliburllib.urlretrieve('ftp://server/path/to/file', 'file')# if you need to pass credentials:#   urllib.urlretrieve('ftp://username:password@server/path/to/file', 'file')

要么:

import shutilimport urllib2from contextlib import closingwith closing(urllib2.urlopen('ftp://server/path/to/file')) as r:    with open('file', 'wb') as f:        shutil.copyfileobj(r, f)

Python3:

import shutilimport urllib.request as requestfrom contextlib import closingwith closing(request.urlopen('ftp://server/path/to/file')) as r:    with open('file', 'wb') as f:        shutil.copyfileobj(r, f)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5673417.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存