Problem making python script request administrator permission

Asked

Viewed 667 times

1

Use Python 3.5 on Windows 8 and a simple script presented problem: I want to copy a file from my desktop to C: Windows folder. First I tried that:

from shutil import copy

path1='C:\\Users\\username\\Desktop\\registro.py'
path2='C:\\Windows\\oi.txt'
copy(path1,path2)

However, it was an error of permission. So I added an excerpt to the script that causes the script to request administrator permission when executed:

from shutil import copy
import sys,os,win32com.shell.shell

if sys.argv[-1]!='asadmin':
    script=os.path.abspath(sys.argv[0])
    params=' '.join([script]+sys.argv[1:]+['asadmin'])
    win32com.shell.shell.ShellExecuteEx(lpVerb='runas',lpFile=sys.executable,lpParameters=params)

path1='C:\\Users\\username\\Desktop\\registro.py'
path2='C:\\Windows\\oi.txt'
copy(path1,path2)

When running such a script, the administrator permission window actually appears, but even giving the permission, the error persisted. So I tried to run line by line in the Python shell running as an administrator (right-clicking) and it worked. Why didn’t it work when I made the request by the script itself? There is another way to make this request by the script?

  • Have you tried copying to the desktop itself or some user folder to see if it gives the same problem?

  • I tried, and it’s no problem. @leonardopessoa

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.