复制字体文件至C盘遇Operation not permitted错误,求管理员运行脚本方案
解决复制字体到Windows字体目录的权限问题
写入C:\Windows\Fonts属于系统级操作,必须以管理员权限运行脚本才能执行,以下是不同类型脚本的处理方案:
批处理脚本(.bat/.cmd)
- 直接右键点击你的批处理文件,选择以管理员身份运行
- 要是想让脚本自动弹窗请求管理员权限,在脚本最开头加这段代码:
@echo off net session >nul 2>&1 if %errorLevel% == 0 goto admin powershell -Command "Start-Process cmd -ArgumentList '/c %~f0' -Verb RunAs" exit /b :admin # 替换成你的复制命令,比如: copy "D:\fonts\example.ttf" "C:\Windows\Fonts\"
PowerShell脚本(.ps1)
- 手动提权:右键点PowerShell图标,选以管理员身份运行,然后输入
.\你的脚本名.ps1执行 - 自动提权:在脚本开头加入这段检测代码:
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) if (-not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { Start-Process powershell.exe "-File `"$PSCommandPath`"" -Verb RunAs exit } # 复制字体的核心代码 Copy-Item -Path "D:\fonts\example.ttf" -Destination "C:\Windows\Fonts\" -Force
Python脚本
- 手动提权:右键打开管理员权限的命令提示符/PowerShell,输入
python 你的脚本名.py执行 - 自动提权:用ctypes模块在脚本内触发权限请求:
import ctypes import sys import shutil def is_admin(): try: return ctypes.windll.shell32.IsUserAnAdmin() except: return False if is_admin(): shutil.copy("D:\fonts\example.ttf", "C:\\Windows\\Fonts\\") else: ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
权限错误提示截图:
Operation not permitted 错误截图
内容的提问来源于stack exchange,提问作者Mandar Hambhire
相关产品推荐
相关产品推荐

