【发布时间】:2021-07-08 21:08:22
【问题描述】:
我正在提取一个 gz 文件,它给了我一个 errno,因为在我也在做的 scraping 中,浏览器还没有下载该文件,所以我想尝试一下,如果错误出现然后我让它等待一段时间并将其放回提取但我做错了,甚至没有接近诚实,我正在这样做:
try:
pass
## Download to folder and use whatever comes out
file_downloaded = False
while not file_downloaded:
for file in os.listdir("tmp"):
if file.endswith("csv.gz"):
fp = os.path.join('tmp', file)
logmsg = ("Extracting ", fp)
instalog.appendMessage('INFO', logmsg)
with gzip.open(fp, 'r') as f_in:
with open(newfile_path, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
file_downloaded = True
elif file.endswith(".csv"):
fp = os.path.join('tmp', file)
logmsg = ("File was not compressed ", fp)
instalog.appendMessage('INFO', logmsg)
with open(fp, 'rb') as f_in:
with open(newfile_path, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
file_downloaded = True
except IOError:
time.sleep(2)
pass
建议我现在这样做
for file in os.listdir("tmp"):
file_downloaded = False
while not file_downloaded:
try:
if file.endswith("csv.gz"):
fp = os.path.join('tmp', file)
logmsg = ("Extracting ", fp)
instalog.appendMessage('INFO', logmsg)
with gzip.open(fp, 'r') as f_in:
with open(newfile_path, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
file_downloaded = True
elif file.endswith(".csv"):
fp = os.path.join('tmp', file)
logmsg = ("File was not compressed ", fp)
instalog.appendMessage('INFO', logmsg)
with open(fp, 'rb') as f_in:
with open(newfile_path, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
file_downloaded = True
except IOError:
time.sleep(5)
【问题讨论】:
-
我不确定我是否理解您的问题。您是在问如何将异常处理放入循环中?
-
如果出现错误 errno,我想重新尝试下载 gz,但请等待几秒钟再重试!