num<文件数量+1
d中填写存储路径
修改newName改变命名规则
import os
import requests
num = 1
while num < 170:
url = 'http://example.com/' + str(num) + '.zip'
d = 'D:\\B\\'
path = d + url.split('/')[-1]
try:
if not os.path.exists(d):
os.mkdir(d)
if not os.path.exists(path):
r = requests.get(url)
r.raise_for_status()
with open(path, 'wb') as f:
f.write(r.content)
f.close()
print("保存成功" + str(num))
else:
print("已存在" + str(num))
except:
print("获取失败" + str(num))
num += 1
path = input(d)
fileList = os.listdir(path)
n = 0
for i in fileList:
# 设置旧文件名(就是路径+文件名)
oldName = path + os.sep + fileList[n] # os.sep添加系统分隔符
# 设置新文件名
newName = path + os.sep + 'a' + str(n + 1) + '.JPG'
os.rename(oldName, newName) # 用os模块中的rename方法对文件改名
print(oldName, '======>', newName)
n += 1