Hardware tip, not code
Instead of a regular Micro USB lead, use a USB Y cable to power your RasPi. One of these or these, with your existing cable.
Coupled with a USB battery like this, this will enable you to move your RasPi without having to power it down. Plug the second USB plug into the battery, and remove the the first from the PSU - instant mobile RasPi!. Plug it back in in its new location and you're done.
You can also use the lead with 2 USB PSUs for twice the current, to run bus-powered accessories.