键盘中断 handeler 在 pycharm 中不起作用



我正在尝试使用 CTRL F2 退出该代码或通过停止按钮关闭进程,但从未调用 finally 子句。我想当我停止将数据写入文件的过程时。

for root, dirnames, files in os.walk(r"c:ahmedSpeedFT-meter12peakdata"):
for name in files:
timeStamps.append(name.replace('peakdata_','').replace('.bz2',''))
print(name.replace('peakdata_','').replace('.bz2',''))
i  = 0
try:
for dirpath, dirnames, filenames in os.walk(r"C:ahmedSpeedFT-meter12peakdata"):
for filename in [f for f in filenames if f.endswith(".bz2")]:
path = os.path.join(dirpath, filename)
print(path)
process_data(path, i)
except KeyboardInterrupt:
pass
finally:
print("writing data")
general_pd['TimeStamp'] = TimeStamp
general_pd['S_strain_HOY'] = S1
general_pd['S_strain_HMY'] = S2
general_pd['S_strain_HUY'] = S3
general_pd['S_strain_ROX'] = S4
general_pd['S_strain_LOX'] = S5
general_pd['S_strain_LMX'] = S6
general_pd['S_strain_LUX'] = S7
general_pd['S_strain_VOY'] = S8
general_pd['S_temp_HOY'] = T1
general_pd['S_temp_HMY'] = T2
general_pd['S_temp_HUY'] = T3
general_pd['S_temp_LOX'] = T4
general_pd['S_temp_LMX'] = T5
general_pd['S_temp_LUX'] = T6
writer = pd.ExcelWriter(r'c:ahmedmedian_data.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
general_pd.to_excel(writer, sheet_name='Sheet1')
# Close the Pandas Excel writer and output the Excel file.
writer.save()

您可能会错过 except/finally 关闭的原因是您正在中断脚本的 try/except (即在循环期间for name in files:(

使用最少的代表性示例调试任何问题总是有帮助的,请尝试运行/停止:

try:
time.sleep(100)
except KeyboardInterrupt:
print('Graceful stop')
finally:
print('Cleanup')

如果您仍然遇到此问题,请向您的 PyCharm 版本提供完整的异常消息。

相关内容

  • 没有找到相关文章

最新更新