2

I am beginner to python and I have a question which is I run the python program which is a program for saving a log file from gz format into csv format, but unfortunately when I try to terminate the program, I terminate the program forcefully, which makes the file not to be written in my computer, so how to terminate manually without deleting a file. Below is my code:

# 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
  • 2
    have you tried to explicitly `open()` the file, thus not using `with` and `flush()` the written content on every gzfile-iteration ? – mzoll May 08 '18 at 07:42
  • Have a look at this: https://stackoverflow.com/questions/3167494/how-often-does-python-flush-to-a-file – ma3oun May 08 '18 at 07:50
  • I think you should close the program gracefully you can capture the `crtl-c` signal and then close the file gracefully https://stackoverflow.com/questions/1112343/how-do-i-capture-sigint-in-python – Hamuel May 08 '18 at 07:53

0 Answers0