[root@iZky7h4oconc7uZ workingcopy_blog]# python3 download_img.py 'https://icxzl.com/wp-content/uploads/2023/03/centos-7-upgrades-python-3.12.png' Image downloaded and saved as ![](../uploads/2024-04-23-17-57-00.png) in /www/wwwroot/workingcopy_blog/source/uploads
# 打开文件以二进制写入模式 withopen(filepath, 'wb') as file: # 将响应内容写入文件 for chunk in response.iter_content(chunk_size=8192): file.write(chunk)
print(f"Image downloaded and saved as {filename} in {directory}")
except requests.RequestException as e: print(f"Error occurred while downloading image: {e}")
if __name__ == "__main__": # 检查命令行参数数量 iflen(sys.argv) < 2orlen(sys.argv) > 3: print("Usage: python script_name.py <image_url> [directory_path]") print("If [directory_path] is not provided, the current script directory will be used.") sys.exit(1)