import time
import requests
import os
int_path = r"F:\image\dcm"
for root, dirs, files in os.walk(int_path):
for filename in files: # 遍歷所有文件
txt_path = os.path.join(root, filename)
print(txt_path)
dcm_name = filename.split(".txt")[0]
fopen = open(txt_path, 'r')
lines = str(fopen.readlines())
all = lines.split(".dcm")
n=0
for e in all:
n+=1
url = e.split("https")[-1]
new_url = "https" + url + ".dcm"
file_name = url.split("/")[-1]+".dcm"
print(new_url)
print(file_name)
if os.path.exists(os.path.join(os.path.join("./test", dcm_name), file_name)):
print("已存在")
pass
else:
try:
res = requests.get(new_url)
with open(os.path.join(os.path.join("./test", dcm_name), file_name), 'wb') as f:
f.write(res.content)
#urlretrieve(new_url, file_name)
time.sleep(1)
except:
print("產生異常")