I need to run a python code that takes several hours and my computer disconnects from the ssh after a certain amount of inactive time. 
I have tried python  test.py > output.txt & but my output file is empty. However, the python code "test" is still running after I log off and log back in to the ssh. I also tried python -u test.py > output.txt &  which does write to the output.txt but it does not continue after the ssh connection is lost. 
I am very new to Linux so I do not know very many commands. I need the simplest/easiest to understand method. 
Thanks!
You can use screen, as Robin Krahl recommended, or you can just run your command with nohup, which suppresses the SIGHUP (hangup) signal from your SSH session disconnecting.
nohup "python -u test.py > output.txt" &
Running python code in background