python - 如何在不删除文件的情况下终止python编程过程
问题描述
我是 python 的初学者,我有一个问题是我运行 python 程序,该程序用于将日志文件从 gz 格式保存为 csv 格式,但不幸的是,当我尝试终止程序时,我强制终止程序,这使文件不会写入我的计算机,因此如何在不删除文件的情况下手动终止。下面是我的代码:
# List of all gz files
gz_files = (gz for gz in glob.glob(os.path.join(GZ_DIR, '*.gz')))
# Loop through all gz files
for gz_file in gz_files:
sql_file = gz_file[:-3]
sql_file = sql_file[:-4] + '.csv'
with open(sql_file, 'wb') as out_file:
with gzip.open(gz_file, 'rb') as in_file:
while True:
chunk = in_file.read(1024)
if not chunk:
break
out_file.write(chunk)
# Step 2: import to sql
import_sql(out_file, DB_HOST, DB_USER, DB_PASS, DB_NAME)
# Step 3: remove uncompresed file
# os.remove(sql_file)
# Step 4: in loop, back to step 1 automatically for another gz files
解决方案
推荐阅读
- php - 连接字符串正确时与 MySQL 的 PHP 连接问题(SQL STATE 2002)
- google-cloud-ml - 如何检查 Google AutoML Tables 模型的结构?
- regex - 正则表达式匹配一组单词,其前面没有,在 25 个字符内,另一组单词
- android - Android SQLite:这是否可以避免插入相同的变量
- python - 如何在 python 中用方形单元格显示网格?
- javascript - 如何将数据库数据放入数组Vue.js
- angular - Visual Studio CODE for Angular CLI 版本 9.1.1 中的 angular.json 错误中的“属性不允许”
- flutter - 我在颤动中显示网格视图有问题
- django - 将熊猫上传到 django 视图时读取文件时出现问题
- c# - 如何在构建 XDocument 时执行 LINQ 查询?