-3

Using the code below I would like to move these files to the Program Files directory. If I try the code below I get this error. How can you request admin privileges like other programs do?

import os 
import glob
import shutil


files = glob.glob("Files/*")
for f in files:
    shutil.copy(f, "C:/Program Files/Project/Output/")

The Error:

PermissionError: [Errno 13] Permission denied: 'C:/Program Files/Projects/Output\\Output.txt'

I am using Python 3.8.5

Arjuna
  • 188
  • 1
  • 11

3 Answers3

0

To edit files in this directory you have to use admin rights. Without them you will always receive a permission error like this:

PermissionError: [Errno 13] Permission denied: 'C:/Program Files/Project/Output/a.txt'

To fix this simply run cmd as administrator and run the command directly from the privileged command prompt.

Tranbi
  • 11,407
  • 6
  • 16
  • 33
E.Murphy
  • 25
  • 1
  • 3
  • hi thanks for helping but i want to give this file online (dont ask why) and i dont want everybody to have to run a cmd as admin and inside the cmd type the command there self – Arjuna Sep 26 '20 at 10:38
  • The system doesn't care how you would want it to work. You have two options: Learn how the system secures file system objects and navigate within those rules, or continue to waste time without getting anywhere. – IInspectable Sep 26 '20 at 13:33
  • @IInspectable just a little update, I made this post a very long time ago and I get that I was very unclear with my question, all I wanted to do was request admin privileges (which can be done by using Pyinstaller and adding --uac=admin). I have now updated the question, If you feel it is a good question now please upvote it, it's very annoying having a very old question making me unable to ask new questions. – Arjuna Jul 06 '23 at 10:12
0

I found the solution

When converting the .py file to an .exe file with PyInstaller, use the option

-uac=admin 

Thanks to @mogi

Arjuna
  • 188
  • 1
  • 11
-1

You need to run the script as administrator (right-click and run as admin).

But why would you want to put the files in program files? If you just want the files to be accessible from start you can add the folder to PATH.

If you want the application to ask for these permissions you can make an executable using py to exe, and then make it ask for the permissions using windows UAC api

Mogi
  • 596
  • 1
  • 6
  • 18
  • This is the reason permissions works like that.. The operating system don't want you to put stuff in program files out of the blue.. it could be malicious or harmful to the user without his knowledge. Please add more information about your need so we can help you better – Mogi Sep 26 '20 at 10:41
  • @RaGe: A script or program cannot increase its own privileges, otherwise whether or not an executable initally had them would provide no security — think about it… – martineau Sep 26 '20 at 10:57
  • I updated the answer for more option, and I wasn't the one who down voted, please don't make hasty conclusions next time when people are volunteering to help you (and BTW stackoverflow has a pretty straight forward guide for when to downvote, I guess the person who downvoted thought your question wasn't clear- as it is..) – Mogi Sep 26 '20 at 11:05
  • for further reading lookup tutorials about `py to exe` `create elevated exe` and read about permissions in operating systems. Please approve the question if it solved your problem – Mogi Sep 26 '20 at 11:16
  • press the `V` under the voting buttons: https://meta.stackexchange.com/questions/86978/how-do-i-accept-an-answer-on-stackoverflow – Mogi Sep 26 '20 at 11:25
  • @martineau just a little update, I made this post a very long time ago and I get that I was very unclear with my question, all I wanted to do was request admin privileges (which can be done by using Pyinstaller and adding --uac=admin). I have now updated the question, If you feel it is a good question now please upvote it, it's very annoying having a very old question making me unable to ask new questions. – Arjuna Jul 06 '23 at 10:21