0

Possible Duplicate:
Request UAC elevation from within a Python script?

I have to copy a file (Say a txt) to a System folder , is there a way to do that ?

this is code i used for normal file transfer and it worked :

import os

import shutil

fileList = os.listdir('J:\hawa')

fileList = ['J:\hawa'+ '\lol.txt']

for f in fileList:
    shutil.copy2(f, 'C:\Windows\System32\Drivers\etc')

but when target directory is system folder the error appears :

Traceback (most recent call last):
 File "C:/Python27/copy2.py", line 8, in <module>
 shutil.copy2(f, 'C:\Windows\System32\Drivers\etc')
 File "C:\Python27\lib\shutil.py", line 128, in copy2
 copyfile(src, dst)
 File "C:\Python27\lib\shutil.py", line 83, in copyfile
 with open(dst, 'wb') as fdst:
IOError: [Errno 13] Permission denied: 'C:\\Windows\\System32\\Drivers\\etc\\lol.txt'

can i just modify the code some way or the other to make it run as admin privilege ?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
ramailo sathi
  • 357
  • 1
  • 4
  • 22
  • 10
    No, otherwise privileges would be pointless as they would be easily bypassed. Instruct the user they need to run the application as an administrator. – Gareth Latty Nov 28 '12 at 19:46
  • 3
    http://stackoverflow.com/questions/130763/request-uac-elevation-from-within-a-python-script – Alex W Nov 28 '12 at 19:50
  • 1
    Yes you can do it. You just need to add the `import please` statement and then do `please(shutil.copy2(f, "afile"))` ;-) – jdi Nov 28 '12 at 19:51
  • 1
    @jdi: there is `please` module on pypi. Does it request UAC elevation on Windows (something like `gksudo` on Ubuntu)? – jfs Nov 28 '12 at 19:55
  • @J.F.Sebastian: Lol, that was a joke. It was to complement Lattyware's comment that it would defeat the purpose of privileges if it could just bypass. I am sure there are modules to induce a user request for approval. – jdi Nov 28 '12 at 20:07
  • @J.F.Sebastian: I believe `please` is there to provide cross-platform, nicely-formatted, colorful command-line interactions… so you could use it to nicely ask the user to quit and re-run the app as an administrator, I suppose. – abarnert Nov 28 '12 at 20:18
  • @jdi: I know that you thought it were a joke. But there is [`antigravity` module](http://svn.python.org/view/python/trunk/Lib/antigravity.py?view=markup&pathrev=66902) inspired by ["antigravity" xkcd comic](http://xkcd.com/353/) so someone also could have implemented ["sudo" ("IT jargon" for 'please') xkcd comic](http://xkcd.com/149/) – jfs Nov 28 '12 at 20:20
  • Apparently I am not funny today. I apologize for my joke JF – jdi Nov 28 '12 at 20:55
  • Just running IDLE as administrator did the job ! Sorry guys ! it was fun though seeing your replies! – ramailo sathi Nov 28 '12 at 21:29

0 Answers0