title: Python - running select sections of code as sudo
body: <p>Is it possible to have a script that can be run without root access, but if root access is required, a password can be collected, and select pieces of the code can be run as sudo? I could use the password and subprocess to run system commands one by one as root if necessary. However, I am using a library that was meant for scripts and requires each script to be run with root privileges. If I could just do something like</p>
  
  <pre><code>runAsRoot(blackBoxFunction, args)
  </code></pre>
  
  <p>I would be set. I really only have one block of code that I need root access for. I don't want to edit the entire library to fit my needs. I could do a workaround, where I generate a script and then execute the script as sudo using subprocess, but this seems prone to error and will be pain with how I set everything up, not to mention I might have to generate scripts on the fly have one great script that can handle every possible scenario.</p>