如何在不删除文件的情况下终止python编程过程

时间:2018-05-08 07:38:15

标签: 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

0 个答案:

没有答案