python连接ftp,自动下载匹配字符的文件的脚本
时间: 2024-04-10 08:33:04 浏览: 132
你可以使用Python的ftplib库来连接FTP服务器并下载匹配字符的文件。以下是一个示例脚本:
```python
from ftplib import FTP
def download_files(hostname, username, password, remote_path, local_path, match_string):
ftp = FTP(hostname)
ftp.login(username, password)
ftp.cwd(remote_path)
files = ftp.nlst()
for file in files:
if match_string in file:
local_file = local_path + '/' + file
with open(local_file, 'wb') as f:
ftp.retrbinary('RETR ' + file, f.write)
print(f"Downloaded file: {file}")
ftp.quit()
# 用法示例
hostname = 'FTP服务器地址'
username = '用户名'
password = '密码'
remote_path = '远程目录'
local_path = '本地目录'
match_string = '匹配字符串'
download_files(hostname, username, password, remote_path, local_path, match_string)
```
请将其中的`hostname`,`username`,`password`,`remote_path`,`local_path`和`match_string`替换为你自己的实际值。这个脚本会连接到FTP服务器,列出远程目录中的文件,并下载文件名中包含匹配字符串的文件到本地目录。
阅读全文