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>Execute the code as a user that only owns that specific directory and has no permissions anywhere else?</p>

<p>However- if you do not completely trust the source of code, you should simply not be using <code>exec</code> under any circumstances.  Remember, say you came up with a python solution... the exec code could literally undo whatever restrictions you put on it before doing its nefarious deeds.  If you tell us the problem you're trying to solve, we can probably come up with a better idea.</p>

python python-2.7 python-exec