Restrict python exec acess to one directory
<p>I have a python script which executes a string of code with the <em>exec</em> function. I need a way to restrict the read/write access of the script to the current directory. How can I achieve this?</p>

<p>Or, is there a way to restrict the python script's environment directly through the command line so that when I run the interpreter, it does not allow writes out of the directory? Can I do that using a virtualenv? How?</p>

<p>So basically, my app is a web portal where people can write and execute python apps and get a response - and I've hosted this on heroku. Now there might be multiple users with multiple folders and no user should have access to other's folders or even system files and folders. The permissions should be determined by the user on the nodejs app (a web app) and not a local user. How do I achieve that? </p>

<p>The question boils down to: How can I safely execute the code I don't trust.<br>
You can't.<br>
Either you know what the code does or you don't execute it.<br>
You can have an isolated environment for your process, for example with docker. But the use cases are far away from executing unsafe code.</p>

python python-2.7 python-exec