Python脚本通过FTP上传文件
我想编写一个脚本将文件上传到FTP。
登录系统将如何工作?我正在寻找这样的东西:
ftp.login=(mylogin)
ftp.pass=(mypass)
以及任何其他登录凭据。
-
使用
ftplib
,您可以这样编写:import ftplib session = ftplib.FTP('server.address.com','USERNAME','PASSWORD') file = open('kitten.jpg','rb') # file to send session.storbinary('STOR kitten.jpg', file) # send the file file.close() # close file and FTP session.quit()
ftplib.FTP_TLS
如果FTP主机需要TLS,请改用。
要检索它,可以使用
urllib.retrieve
:import urllib urllib.urlretrieve('ftp://server/path/to/file', 'file')
编辑 :
要查找当前目录,请使用
FTP.pwd()
:FTP.pwd():返回服务器上当前目录的路径名。
要更改目录,请使用
FTP.cwd(pathname)
:FTP.cwd(pathname):设置服务器上的当前目录。