I use Python 3.5 on Windows 8 and a simple script presented problem: I want to copy a file from my desktop to the C: \ Windows folder. First I tried this:
from shutil import copy
path1='C:\Users\username\Desktop\registro.py'
path2='C:\Windows\oi.txt'
copy(path1,path2)
However, it gave permission error. So I added to the script a snippet that causes the script to request administrator permission when it runs:
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 executing such a script, the administrator permission window actually appears, however, even giving the permission, the error persisted. So I tried to run row by line in the Python shell run as administrator (right click) and it worked. Why did not it work when I applied for the script itself? Is there another way to do this request through the script?