允许 Python 以管理员权限访问 Program Files 文件夹

问题描述 投票:0回答:0

我有这个程序可以将所有文件从名为

src
的文件夹中复制出来,并创建一个名为
C:\Program Files\my_program
的目录,然后将它们粘贴到其中。接下来,它在
C:\ProgramData\Microsoft\Windows\Start Menu\Programs
中为
C:\Program Files\my_program
中的可执行文件创建一个快捷方式。我用过
ctypes
图书馆 以及获得程序管理员访问权限的“提示”操作:
ctypes.windll.shell32.ShellExecuteW(None, "runas", "python", "my_program.py", None, 1) <= 32
。 但它不起作用,它返回这个错误:
PermissionError: [Errno 13] Permission denied: 'C:\\Program Files'
.

到目前为止,这是我的代码:

from winshell import Shortcut
import os
import shutil
import ctypes

ctypes.windll.shell32.ShellExecuteW(None, "runas", "python", "my_program.py", None, 1) <= 32

cwd = os.path.dirname(os.path.abspath(__file__))
full_cwd = cwd.replace("\\", "\\")

source_folder = full_cwd + "\\src"
destination_folder = "C:\\Program Files\\my_program"

if not os.path.exists(destination_folder):
    os.makedirs(destination_folder)

for filename in os.listdir(source_folder):
    shutil.copy(os.path.join(source_folder, filename), destination_folder)

shortcut_path = os.path.join("C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs", "my_program.lnk")
shortcut = Shortcut(path=shortcut_path)

shortcut.path = os.path.join(destination_folder, "my_program.exe")
shortcut.working_directory = destination_folder
shortcut.icon_location = (os.path.join(destination_folder, "icon.ico"), 0)
shortcut.description = "my_program"

shortcut.show_cmd = Shortcut.SW_HIDE

shortcut.save()

有人知道如何解决这个问题吗?

python directory admin permission-denied
© www.soinside.com 2019 - 2024. All rights reserved.