• PermissionError: [WinError 5] 拒绝访问。


    在编写python脚本文件test.py时

    编写了如下的创建文件夹方法:

     1 def make_folder(folder_name):
     2     FOLDER_NOT_EXIST = False
     3     while (True):
     4         if FOLDER_NOT_EXIST == True or folder_name not in files:
     5             os.mkdir(folder_name)
     6             FOLDER_NOT_EXIST = FALSE
     7             print(folder_name + 'successfully created')
     8             break
     9         else:
    10             print('Folder ' + folder_name + ' already exist.')
    11             isdel = input('Enter Y/N decide overwrite it or not:')
    12             if isdel == 'Y':
    13                 os.remove(folder_name)
    14                 FOLDER_NOT_EXIST = True
    15             if isdel == 'N': break
    16             continue

    本意为利用os.remove来规避可能遇到的FileExistsError

    但由于程序中的os.remove()需要提升系统权限为administrator才能执行

    所以如果直接使用cpython运行代码:

     python test.py 

    就会得到:

    PermissionError: [WinError 5] 拒绝访问。

    在win10系统下让程序得到administrator权限运行不是一件容易的事,因此在win10环境下要尽量规避此类代码

    取而代之的方法是:

    程序中删除os.remove相关的代码,而在遇到FileExistsError时,cmd当前目录下运行

     rmdir /s /q [dirname] 

    再运行

     python test.py 

    就可以成功运行了

  • 相关阅读:
    intellij idea 为JavaEE项目建立Servlet
    DNS无响应
    取消Gridvie中button的焦点
    android.os.NetworkOnMainThreadException
    java.net.MalformedURLException: Protocol not found:
    Failure [INSTALL_FAILED_OLDER_SDK]
    android library projects cannot be launched
    CDQ分治的嵌套
    CDQ分治入门
    最远 Manhattan 距离
  • 原文地址:https://www.cnblogs.com/J14nWe1/p/14388239.html
Copyright © 2020-2023  润新知