我正在尝试将文件从我的 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/