【问题标题】:PermissionError: [WinError 5] Access is denied - Running the program in vs codePermissionError: [WinError 5] Access is denied - 在 vs code 中运行程序
【发布时间】:2021-03-23 03:02:04
【问题描述】:

我正在尝试编写一个程序来删除我系统上的所有临时文件。我使用来自python 3.9.1os module,我正在VS Code 上运行它。我试图找出解决方案,建议在Administrator mode 中运行终端,但我使用的是 vs 代码。我发现没有可以更改用户模式的功能。 下面是代码,

import os
import shutil

os.chdir('c:\\Windows\\Temp')
for folderNames,subfolderNames,fileNames in os.walk('c:\\Windows\\Temp'):
    print('The folder is '+ folderNames)
    print('The subfolder in '+folderNames+' are: '+str(subfolderNames))
    print('The filenames in '+folderNames+' are:'+str(fileNames))
    print()

    for file in fileNames:
        os.unlink(file)
        #print(file)
    for folder in subfolderNames:
        shutil.rmtree(subfolderNames)
        #print(os.path.join('c:\\Windows\\Temp\\'),folder)



它给了我这个错误:PermissionError: [WinError 5] Access is denied: 'adobegc.log'

你能帮忙吗...

谢谢 斯韦塔

【问题讨论】:

    标签: python file operating-system shutil


    【解决方案1】:

    您可以尝试使用runas 命令以管理员模式运行该文件。

    runas /user:Administrator your_Command
    

    如果您的命令包含空格,请不要忘记添加引号。

    runas /user:Administrator "your command"
    

    【讨论】:

    • 感谢您的回复 :) 我正在尝试在 vs 代码中运行此程序,那么我将如何编写命令?你能澄清一下吗?
    • 在 VScode 终端上运行该命令,该命令将以提升的权限运行。假设您将 python 代码保存在一个名为“delete.py”的文件中。要运行此文件,您需要在 VScode 终端中执行命令 python delete.py。要在管理员模式下运行此命令,请转到 VScode 终端并运行此命令:runas /user:Administrator "python delete.py"。希望这能澄清您的疑问。
    • 谢谢...我会试试这个:)
    猜你喜欢
    • 2021-01-10
    • 1970-01-01
    • 2016-07-21
    • 2017-08-14
    • 2020-06-20
    • 2022-01-25
    • 2020-07-22
    • 1970-01-01
    相关资源
    最近更新 更多