本文介绍了为什么os.rename有时会返回错误访问被拒绝python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 os.rename() 方法在我的 python 脚本中重命名目录.该脚本每天由调度程序自动调用.有时 os.rename() 函数返回错误,

I used os.rename() method to rename the directory in my python script. This script called automatically by the scheduler every day. Sometimes the os.rename() function returns the error,

 [Error 5] Access is denied

但所有其他时间都可以正常工作.代码,

But all other times its working fine.Code,

try:
  if(os.path.exists(Downloaded_Path)):
    os.rename(Downloaded_Path, Downloaded_Path + "_ByClientTool")
except Exception,e:
  print "Error !!", str(e)
  return 1

推荐答案

我在 Windows 10 上遇到了类似的问题:有时我的 python 脚本无法重命名目录,即使我可以毫无问题地手动重命名它.

I had a similar problem on Windows 10: sometimes my python script could not rename a directory even though I could manually rename it without a problem.

我使用 Sysinternal 的 handle.exe 工具发现 explorer.exe 有一个指向我试图重命名的目录的子目录的句柄.事实证明,资源管理器将此子目录添加到其快速访问"部分,这阻止了我的脚本重命名文件夹.

I used Sysinternal's handle.exe tool to find that explorer.exe had a handle to a sub-directory of the directory I was trying to rename. It turns out explorer was adding this sub-directory to its "Quick Access" section which prevented my script from renaming the folder.

我最终从资源管理器 -> 查看 -> 选项 -> 常规 -> 隐私中禁用了在快速访问中显示常用文件夹"选项.

I wound up disabling the "Show frequently used folders in Quick access" option from Explorer -> View -> Options -> General -> Privacy.

这篇关于为什么os.rename有时会返回错误访问被拒绝python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 23:30