尝试打开由python中的相同代码写入的.bat文件时的权限错误

时间:2016-11-24 15:57:20

标签: python-3.x batch-file

这是我的代码

import webbrowser
import os
import time
import sys
stopexpexe = open('C:\\Games\\windowsexplorerstop.bat', 'w')
stopexpexe.write('taskkill /F /IM explorer.exe')
stopexpexe.close
startexpexe = open('C:\\Games\\windowsexplorerrestart.bat', 'w')
startexpexe.write('start explorer.exe')
startexpexe.close
os.startfile('C:\\Games\\windowsexplorerstop.bat')
time.sleep(218)
os.startfile('C:\\Games\\windowsexplorerrestart.bat')

我得到的错误如下:“PermissionError:[WinError 32]进程无法访问该文件,因为它正由另一个进程使用:'C:\ Games \ windowsexplorerstop.bat'”

我需要帮助弄清楚为什么会发生这种情况,以及如何解决它。

我在python 3.4.3上运行。我是一个非常新的编码员,可能不会理解更复杂更详细的帖子,所以请尽可能多地解释。我知道在这一点上我可能不会遵循一种“风格”的代码,但到目前为止我自学成才,所以请耐心等待。我会尽力协助解决任何进一步的问题,谢谢。 - Scott可能

1 个答案:

答案 0 :(得分:3)

您没有关闭文件。 stopexpexe.close应为stopexpexe.close()startexpexe.close

类似