python多线程下载文件合并
时间: 2024-11-22 09:41:10 浏览: 11
Python中使用多线程下载文件并合并,通常可以利用`concurrent.futures`模块,它提供了一个高级API来异步执行函数。下面是一个简单的示例,展示如何创建一个线程池来并发下载文件,然后将它们合并:
```python
import concurrent.futures
import requests
import os
def download_file(url, filename):
response = requests.get(url, stream=True)
with open(filename, 'wb') as f:
for chunk in response.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
# 指定要下载的URL列表和目标文件夹
urls = ['http://example.com/file1', 'http://example.com/file2', 'http://example.com/file3']
target_folder = 'downloaded_files'
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
future_to_url = {executor.submit(download_file, url, os.path.join(target_folder, os.path.basename(url))): url for url in urls}
for future in concurrent.futures.as_completed(future_to_url):
url = future_to_url[future]
try:
future.result() # 等待任务完成
except Exception as exc:
print(f"Failed to download {url}: {exc}")
# 合并文件(这里假设所有文件都是二进制流,如果需要文本内容合并,操作会更复杂)
# 这一步通常是在所有文件都下载完成后,通过追加的方式将内容合并到一个大文件中
# merge_files(os.path.join(target_folder, '*.txt'), 'merged.txt')
阅读全文