简体   繁体   English

PermissionError: [WinError 5] 访问被拒绝 OS.remove

[英]PermissionError: [WinError 5] Access is denied OS.remove

i am trying to remove the folder variable "name" if its been in the folder longer than X amount of time.如果文件夹变量“名称”在文件夹中的时间超过 X,我正在尝试删除它。 Can i run this script in admin mode without having to "right click" and run as admin?我可以在管理员模式下运行此脚本而无需“右键单击”并以管理员身份运行吗? If i try to automate this script i would need something to that nature.如果我尝试自动化此脚本,我将需要这种性质的东西。 I try using the os.remove function but i get an error below:我尝试使用 os.remove function 但我收到以下错误:

Error错误

PermissionError: [WinError 5] Access is denied:

Code:代码:

for root, folders, files in os.walk('\\\MYDATA\\user$\\test\\Documents\\chris2020\\test.fof'):
for name in folders:
    datetimeFormat = '%Y-%m-%d %H:%M:%S.%f'
    filedate = str(datetime.fromtimestamp(os.path.getmtime(os.path.join(root, name))))
    now_time = str(datetime.now())
    now_time = datetime.strptime(now_time, datetimeFormat)
    filetime = datetime.strptime(filedate, datetimeFormat)
    difference = now_time-filetime
    if difference > timedelta(days=2):
        print(filetime)
        print(difference)
        print('Hi')
        # os.remove('\\\MYDATA\\user$\\test\\Documents\\chris2020\\test.fof\\' + name)
        shutil.rmtree('\\\MYDATA\\user$\\test\\Documents\\chris2020\\test.fof\\' + name)
        file_times = os.path.join("\\\MYDATA\\user$\\test\\Documents\\chris2020\\test.fof\\", name), ": ", str(
            difference)
        file_times_final.append(file_times[0] + file_times[1] + file_times[2])
    else:
        print("None")
        break

Assuming the issue is that Python is not elevated, the solution provided here might be useful.假设问题是 Python 未提升, 此处提供的解决方案可能有用。

To run an external command from within Python, this solution could be suitable:要从 Python 内部运行外部命令, 此解决方案可能适用:

#!python
# coding: utf-8
import sys
import ctypes

def run_as_admin(argv=None, debug=False):
    shell32 = ctypes.windll.shell32
    if argv is None and shell32.IsUserAnAdmin():
        return True

    if argv is None:
        argv = sys.argv
    if hasattr(sys, '_MEIPASS'):
        # Support pyinstaller wrapped program.
        arguments = map(unicode, argv[1:])
    else:
        arguments = map(unicode, argv)
    argument_line = u' '.join(arguments)
    executable = unicode(sys.executable)
    if debug:
        print 'Command line: ', executable, argument_line
    ret = shell32.ShellExecuteW(None, u"runas", executable, argument_line, None, 1)
    if int(ret) <= 32:
        return False
    return None


if __name__ == '__main__':
    ret = run_as_admin()
    if ret is True:
        print 'I have admin privilege.'
        raw_input('Press ENTER to exit.')
    elif ret is None:
        print 'I am elevating to admin privilege.'
        raw_input('Press ENTER to exit.')
    else:
        print 'Error(ret=%d): cannot elevate privilege.' % (ret, )

The key lines seem to be关键线似乎是

import ctypes
shell32 = ctypes.windll.shell32
shell32.ShellExecuteW(None, u"runas", executable, argument_line, None, 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM