gpt4 book ai didi

python - 键盘中断处理程序在 pycharm 中不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 21:39:44 28 4
gpt4 key购买 nike

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

  for root, dirnames, files in os.walk(r"c:\ahmed\SpeedFT-meter12\peakdata"):
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:\ahmed\SpeedFT-meter12\peakdata"):
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:\ahmed\median_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 版本提供完整的异常消息。

关于python - 键盘中断处理程序在 pycharm 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52964118/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com