views:

36

answers:

4

I m using shutil.copy from python to copy a list of files. But when i copy the files to /usr/lib/ location, i m getting permission denied as i need to be an administrator to do that.

So How could i copy files with admin permission or how could i get the admin password from the user to copy the files?

Ideas would be appreciated

A: 

Start your program with a user that is allowed to write there. For example login to root first (su) or run the script with sudo myscript.py.

poke
+2  A: 

Make the user run the script as an administrator:

sudo python-script.py

Unix already has authentication and password management. You don't need to write your own, and there will doubtless be security bugs if you try.

katrielalex
A: 

I have a similar kind of problem. How could i make my python script to ask for admin password to copy the files.

No the script should not be run in the terminal as it would be initiated by other process/application.

Thnx

pytho
@pytho that's a different, i.e. __new__ question. delete this post and go ask it somewhere appropriate. Maybe [here](http://stackoverflow.com/questions/ask) ;)
aaronasterling
A: 

To add to what katrielalex said: you can make the script run itself via sudo if you want. Here's a proof of concept:

import sys, os, subprocess

def do_root_stuff():
    print('Trying to list /root:')
    for filename in os.listdir('/root'):
        print(filename)

if __name__ == '__main__':
    print('Running as UID %d:' % os.geteuid())

    if os.geteuid() == 0:
        do_root_stuff()
    else:
        subprocess.check_call(['sudo', sys.executable] + sys.argv)
Evgeny