Claudia C
Claudia C

Reputation: 11

How to stop a Jupyter Notebook loop without losing the data obtained so far

I'm in a similar situation to Stop a python script without losing data.

I am currently using Jupyter Notebook, the notebook is open and running.

I was wondering exactly how to make the answer work for my case. I am not a programmer, so I would need a step by step if at all possible.

The loop that has been running for 2 days is the following:

data = []
for file_origin in onlyfiles:
    path_to_text = mypath + '/' + file_origin
    data.append(tratamiento_datos(path_to_text))

I would like to retrieve data, saving whatever I can from the loop.

Upvotes: 1

Views: 2603

Answers (2)

Epsi95
Epsi95

Reputation: 9047

Step 1:

Stop the jupyter notebook. Go to kernel --> Interrupt

Step 2:

Save the data to disk.

import json
import time


# run this after stopping   
with open(f'data_1_{int(time.time())}.json', 'w') as f:
    f.write(json.dumps(data))

NOTE: make sure the data in data list is json serializable. Or you can pickle it, whatever you like.

Step 3:

Start it again.

data = []

# start from where you left
index = onlyfiles.index(file_origin)

for file_origin in onlyfiles[index:]:
    path_to_text = mypath + '/' + file_origin
    data.append(tratamiento_datos(path_to_text))

Upvotes: 1

solaluset
solaluset

Reputation: 301

Just stop the runnig cell, everything appended to data will remain there. After that you can save data content to disk with json or whatever way compatible with your data.

Upvotes: 0

Related Questions