如何循环尝试:异常?
How to loop in the try: exception?
我正在提取一个 gz 文件,它给了我一个错误号,因为在 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)
def unzip_gzip(source_path, destination_path):
with gzip.open(source_path, "rb") as f_in:
with open(destination_path, "wb") as f_out:
shutil.copyfileobj(f_in, f_out)
def process_file(file_name):
if file_name.endswith("csv.gz"):
full_path = os.path.join("tmp", file)
instalog.appendMessage("INFO", ("Extracting ", full_path))
unzip_gzip(full_path, new_file_path)
elif file_name.endswith(".csv"):
full_path = os.path.join("tmp", file)
instalog.appendMessage("INFO", ("File was not compressed ", full_path))
shutil.copyfile(full_path, new_file_path)
else:
return
for file in os.listdir("tmp"):
success = False
while not success:
try:
process_file(file)
success = True
except IOError:
time.sleep(2)
我正在提取一个 gz 文件,它给了我一个错误号,因为在 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)
def unzip_gzip(source_path, destination_path):
with gzip.open(source_path, "rb") as f_in:
with open(destination_path, "wb") as f_out:
shutil.copyfileobj(f_in, f_out)
def process_file(file_name):
if file_name.endswith("csv.gz"):
full_path = os.path.join("tmp", file)
instalog.appendMessage("INFO", ("Extracting ", full_path))
unzip_gzip(full_path, new_file_path)
elif file_name.endswith(".csv"):
full_path = os.path.join("tmp", file)
instalog.appendMessage("INFO", ("File was not compressed ", full_path))
shutil.copyfile(full_path, new_file_path)
else:
return
for file in os.listdir("tmp"):
success = False
while not success:
try:
process_file(file)
success = True
except IOError:
time.sleep(2)