如何使用 python 查找文件是否已完全下载?
How to find if a file has been downloaded completely using python?
我们有一个 python 脚本可以自动批处理从 Internet 下载的时间序列图像数据。当前脚本要求在执行前下载所有数据。这会消耗更多时间。我们想通过编写一个调度程序来修改脚本,该调度程序将在完全下载单个数据时调用脚本。如何使用 python 查找文件是否已完全下载?
如果您使用Python下载文件,那么您可以在文件下载操作完成后进行图像处理操作。使用 requests 的示例:
import requests
import mymodule # The module containing your custom image-processing function
for img in ("foo.png", "bar.png", "baz.png"):
response = requests.get("http://www.example.com/" + img)
image_bytes = response.content
mymodule.process_image(image_bytes)
但是,使用上面的顺序方法,您将花费大量时间等待来自远程服务器的响应。为了加快速度,您可以使用 aysncio and aiohttp. There's a good introduction to downloading files this way in Paweł Miech's blog post Making 1 million requests with python-aiohttp 一次下载和处理多个文件。您需要的代码类似于该博客底部的示例 post(带有信号量的代码)。
我们有一个 python 脚本可以自动批处理从 Internet 下载的时间序列图像数据。当前脚本要求在执行前下载所有数据。这会消耗更多时间。我们想通过编写一个调度程序来修改脚本,该调度程序将在完全下载单个数据时调用脚本。如何使用 python 查找文件是否已完全下载?
如果您使用Python下载文件,那么您可以在文件下载操作完成后进行图像处理操作。使用 requests 的示例:
import requests
import mymodule # The module containing your custom image-processing function
for img in ("foo.png", "bar.png", "baz.png"):
response = requests.get("http://www.example.com/" + img)
image_bytes = response.content
mymodule.process_image(image_bytes)
但是,使用上面的顺序方法,您将花费大量时间等待来自远程服务器的响应。为了加快速度,您可以使用 aysncio and aiohttp. There's a good introduction to downloading files this way in Paweł Miech's blog post Making 1 million requests with python-aiohttp 一次下载和处理多个文件。您需要的代码类似于该博客底部的示例 post(带有信号量的代码)。