Let's say there is a server on the internet that one can send a piece of code to for evaluation. At some point server takes all code that has been submitted, and starts running and evaluating it. However, at some point it will definitely bump into "os.system('rm -rf *')" sent by some evil programmer. Apart from "rm -rf" you could expect people try using the server to send spam or dos someone, or fool around with "while True: pass" kind of things.
Is there a way to coop with such unfriendly/untrusted code? In particular I'm interested in a solution for python. However if you have info for any other language, please share.
Unless I'm mistaken (and I very well might be), this is much of the reason behind the way Google changed Python for the App Engine.  You run Python code on their server, but they've removed the ability to write to files.  All data is saved in the "nosql" database.  
It's not a direct answer to your question, but an example of how this problem has been dealt with in some circumstances.
sandbox to execute possibly unfriendly python code