首页 > 解决方案 > 如何在不删除文件的情况下终止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

标签: python

解决方案


推荐阅读