Python 逐行读取txt文件并输出
f = open("url.txt") # 返回一个文件对象
line = f.readline() # 调用文件的 readline()方法
while line:
print (line, end ='') # 加',' 忽略每行最后的换行符
f.close()
Python 批量下载文件并保存
from io import BytesIO
import requests
import os
from PIL import Image
# 获取图片下载图片
def download_img(save_name):
global str
save_path ="image" + '/' + save_name #保存路径名
if not os.path.exists((save_path)):
os.makedirs(save_path)
##1.修改
f = open("url.txt") # 替换自己要下载的路径集合文件:每行为一个URL地址
line = f.readline()
num = 0
while line:
#print(line)
url = str(line).replace("\n","")
print("正在下载 第 %d 张图片...." % num)
num += 1
line = f.readline()
try:
# 获取当前页面的源码
img = requests.get(url,timeout=30)# 获取图片内容
image = Image.open(BytesIO(img.content))
# imagere = image.resize((1280, 768))##如果原图过大就取消注释
# img_name = save_path + '/' + 'Wheat_Powdery_mildew' +str (items) +'.jpg'
##2.修改?
img_name = save_path + '/'+ 'xxx' +str (num) +'.jpg'
image.save(img_name)
# with open (img_name,'wb') as f:
# f.write(img.content)
except Exception as e:
print('无法下载,%s'% e)
continue
print('下载完成!')
##3.##修改
download_img('save_name')
版权声明:本文为qq_35892287原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。