我的Python程序如何避免并引用“允许”:[WinError 5]访问被拒绝。当在“ c:\ program文件”中对文件进行符合链接时;
我的C:Drive(256GB SSD)上的空间不足。
我开始寻找一种将文件移动“ C:\ Program Files”和“ C:\ Program Files(X86)”中的文件的方法。谷歌搜索(永远)之后,我决定编写自己的Python程序,该程序将这些目录将文件复制到目标驱动器,并在原始位置留下符号链接。由于文件在技术上不会移动,因此Windows不应该在乎。
测试后,我开始尝试从“ C:\ Program Files(x86)”中实际移动文件。我将几个子目录移到了更宽敞的驱动器上,似乎没有什么不对的。甚至Winmerge都说,移动目录与原始作品相同,这应该意味着符号链接功能是功能的。
预防措施:
- Python程序由我的帐户从CMD.EXE“作为管理员”运行,以避免特权错误。
- 该计算机已被放置在安全模式下,以避免尝试移动可能正在运行的程序的问题。
- 我的用户帐户具有管理员特权。
- 受控文件夹访问在设置/病毒&威胁保护。
尽管有这些预防措施,但是在尝试将另一个目录从(x86),calibre2移动时,我收到的10个错误类似于以下内容:
Traceback (most recent call last):
File "C:\Users\...\symmer.py", line 254, in _mv
os.remove(ps)
PermissionError: [WinError 5] Access is denied: 'C:\\Program Files (x86)\\Calibre2\\app\\bin\\api-ms-win-core-console-l1-1-0.dll'
错误并非全部.dll。 .dlls,1 .xml文件,1 .pak文件和一个EXE有7个错误。但是他们都是[Winerror 5]。
相关的Python 3.10.2代码很简单:
# Copy source file to destination if not there.
if not Path(dpath).exists():
shutil.copyfile(ps, dpath)
# Delete source file from its directory.
os.remove(ps)
# Create a symlink in the source file's directory to dpath.
# Program needs to run "as admin" on Windows.
os.symlink(dpath, ps)
因此,删除()失败,因为Windows拒绝访问。考虑到上面的预防措施,我看不出这是怎么可能的。我必须直接从管理员帐户运行程序以避免错误吗?这将是一个问题,因为我很确定我在安装Windows时从未将该帐户提供给密码。
I'm running out of space on my C: drive, a 256GB SSD.
I started looking for a way to move the files in "C:\Program Files" and "C:\Program Files (x86)" to another disk. After googling (forever), I decided to write my own Python program that would copy the files from those directories to the destination drive and leave symlinks at the original locations. Since the files won't technically be moving, Windows should not care.
After testing, I began trying to actually move files from "C:\Program Files (x86)". I moved a couple of subdirectories to the roomier drive and nothing seemed amiss. Even WinMerge said the moved directories were identical to their originals, which should mean the symlinks are functional.
Precautions:
- The Python program is run under my account from cmd.exe "as administrator" to avoid privilege errors.
- The computer has been placed in Safe Mode to avoid problems trying to move programs that might be running.
- My user account has Administrator privileges.
- Controlled folder access is OFF in Settings/Virus & threat protection.
In spite of these precautions, when attempting to move another directory from (x86), Calibre2, I am receiving 10 errors similar to the following:
Traceback (most recent call last):
File "C:\Users\...\symmer.py", line 254, in _mv
os.remove(ps)
PermissionError: [WinError 5] Access is denied: 'C:\\Program Files (x86)\\Calibre2\\app\\bin\\api-ms-win-core-console-l1-1-0.dll'
The errors are not all .dlls. There are 7 errors about .dlls, 1 .xml file, 1 .pak file, and an exe. But they are all [WinError 5].
The relevant Python 3.10.2 code is straightforward:
# Copy source file to destination if not there.
if not Path(dpath).exists():
shutil.copyfile(ps, dpath)
# Delete source file from its directory.
os.remove(ps)
# Create a symlink in the source file's directory to dpath.
# Program needs to run "as admin" on Windows.
os.symlink(dpath, ps)
So the remove() is failing because Windows is denying access. I don't see how that is possible given the precautions above. Must I run the program directly from the Administrator account to avoid the error? That would be a problem since I'm pretty sure I never gave that account a password when I installed Windows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
某些程序即使在安全模式下运行。您可以尝试找到可能使用这些文件并停止它们的任何服务,但这可能还不够。就像您说的那样,问题是删除,因此我建议您复制而不是删除。然后,您可以创建一个可引导的Windows USB驱动器并从中启动,以完成删除并创建符号链接。 (您也许可以使用Linux USB驱动器,但其中一些不支持NTF)
Some programs run even in safe mode. You could try to find any services that might be using those files and stop them, but that might not be enough. Like you said, the problem is deletion, so I would recommend copying rather than deleting. Then, you can create a bootable Windows USB drive and boot from that, to finish the deletion and create the symlinks. (You might be able to use a Linux USB drive, but some of them don't support NTFS by default)