import ftplib
# 创建FTP对象
ftp = ftplib.FTP()
# 连接到FTP服务器
ftp.connect('ftp.example.com', 21)
# 登录FTP服务器
ftp.login(user='username', passwd='password')
# 获取当前目录
print("Current directory:", ftp.pwd())
# 列出目录内容
ftp.dir()
# 更改目录
ftp.cwd('/new_directory')
# 上传文件
with open('local_file.txt', 'rb') as file:
ftp.storbinary('STOR remote_file.txt', file)
# 下载文件
with open('downloaded_file.txt', 'wb') as file:
ftp.retrbinary('RETR remote_file.txt', file.write)
# 关闭连接
ftp.quit()
ftplib.FTP() 创建一个 FTP 对象。connect 方法连接到指定的 FTP 服务器,参数为服务器地址和端口号(默认为21)。login 方法登录,参数为用户名和密码。pwd 方法获取当前工作目录。dir 方法列出当前目录下的文件和子目录。cwd 方法更改当前工作目录。storbinary 方法上传文件,参数为命令和文件对象。retrbinary 方法下载文件,参数为命令和回调函数(这里用 file.write 写入文件)。quit 方法关闭与 FTP 服务器的连接。如果有任何问题或需要进一步的帮助,请告诉我!
上一篇:python 执行系统命令
下一篇:python 列表删除
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站