python脚本在Windows中以什么用户身份运行? [英] What user do python scripts run as in windows?

查看:198
本文介绍了python脚本在Windows中以什么用户身份运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让python删除一些目录,但出现访问错误.我认为python用户帐户没有权限?

I'm trying to have python delete some directories and I get access errors on them. I think its that the python user account doesn't have rights?

WindowsError: [Error 5] Access is denied: 'path'

是我运行脚本时得到的.
我已经尝试过

is what I get when I run the script.
I've tried

shutil.rmtree  
os.remove  
os.rmdir

它们都返回相同的错误.

they all return the same error.

推荐答案

即使将文件和目录设置为只读",即使刚刚复制它们,我们也无法在Windows上删除它们. shutil.rmtree()为您提供了某种异常处理程序来处理这种情况.您调用它并提供如下异常处理程序:

We've had issues removing files and directories on Windows, even if we had just copied them, if they were set to 'readonly'. shutil.rmtree() offers you sort of exception handlers to handle this situation. You call it and provide an exception handler like this:

import errno, os, stat, shutil

def handleRemoveReadonly(func, path, exc):
  excvalue = exc[1]
  if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES:
      os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777
      func(path)
  else:
      raise

shutil.rmtree(filename, ignore_errors=False, onerror=handleRemoveReadonly)

您可能想尝试一下.

这篇关于python脚本在Windows中以什么用户身份运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆