python - 如何编写 Python 脚本从 FTP 服务器下载文件

标签 python download ftp server

我正在尝试将文件从我的 FTP 服务器下载到特定文件夹,但没有 GUI。这是我目前所拥有的,但它什么也没做,

import urllib
urllib.urlretrieve('ftp://USERNAME:PASSWORD@ftp.SERVERNAME/File path/', 'FILENAME')

最佳答案

我编辑了我的答案以使其更简单..现在我们需要使用 FtpLib

下面的代码很简单也很优雅:D

import ftplib

path = 'pub/Health_Statistics/NCHS/nhanes/2001-2002/'
filename = 'L28POC_B.xpt'

ftp = ftplib.FTP("Server IP") 
ftp.login("UserName", "Password") 
ftp.cwd(path)
ftp.retrbinary("RETR " + filename ,open(filename, 'wb').write)
ftp.quit()

Just in case you need some explanation:

path is obviously the location of the file in the ftp server

filename is the name + extension of the file you want to download form server

ftp.login is where you'll put your credentials(username, password)

ftp.cwd will change the current working directory to where the file is located in order to download it :)

retrbinary simply will get the file from the server and store in your local machine using the same name it had on the server :)

Do not forget to change Server IP argument to your server's ip

and Voila that's it.

关于python - 如何编写 Python 脚本从 FTP 服务器下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40608183/

相关文章:

python - 如何从 Python 中的图像中获取信噪比?

Python:获取资源管理器中选定文件的列表(Windows 7)

powershell - 我可以使用 Invoke-WebRequest 将二进制数据写入文件并仍然获取状态代码吗?

java - 如何编排批量执行?

git - 克隆远程 Git 存储库(服务器拒绝您更改到给定目录)

java - 使用 Apache Commons Net FTPClient 从 FTP 服务器循环读取多个文件

python - 让 PyC​​harm 在 windows linux 子系统上识别 python(windows 上的 bash)

Java BufferedOutputStream : How many bytes to write

java - 如何通过浏览器提供本地文件

python 3 : how to check if an object is a function?