Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何删除正在运行的Python脚本_Python - Fatal编程技术网

如何删除正在运行的Python脚本

如何删除正在运行的Python脚本,python,Python,我做了一个程序,我想添加一个更新系统。它会检查网站是否有更新,如果有更新,它会下载新文件并覆盖自己的新文件 .py文件可以正常工作。它通过os.unlink方法删除自身,并保存新文件而不是旧文件。然后,它打开新版本并关闭自己。 但是,当我运行其.exe文件pyinstaller时,我得到了一个错误: WindowsError: [Error 32] The process cannot access the file because it is being used by another pro

我做了一个程序,我想添加一个更新系统。它会检查网站是否有更新,如果有更新,它会下载新文件并覆盖自己的新文件

.py文件可以正常工作。它通过os.unlink方法删除自身,并保存新文件而不是旧文件。然后,它打开新版本并关闭自己。 但是,当我运行其.exe文件pyinstaller时,我得到了一个错误:

WindowsError: [Error 32] The process cannot access the file because it is being used by another process
我怎样才能解决它

编辑:我通过pyinstaller将其转换为exe文件,并键入以下命令:

pyinstaller file.py
我的脚本中的代码:

os.unlink(sys.argv[0]) # The program is deleting itself.

if sys.argv[0].endswith(".py"): #detects that is itself an exe or a python file
    urlretrieve("http://version.information.site.com/newprogram.py", "newprogram.py") #download file as newprogram.py
    os.rename("newprogram.py", "program.py") #rename new file as program.py
    os.system("python program.py") #run new program
elif sys.argv[0].endswith(".exe"): #if it is a exe file
    urlretrieve("http://version.information.site.com/newprogram.exe",  "newprogram.exe") #download file as newprogram.exe
    os.rename("newprogram.exe", "program.exe") #rename new file as program.exe
    os.system("program.exe") #run new program

试着用admin perms运行它。问题可能在于如何将其转换为.exe。你能详细说明一下吗?你能不能也发一下你的代码,只是相关的部分。

这可能是一个幸运的猜测,我现在无法在windows上测试它

从进程中解锁文件处理程序是一种令人讨厌的过程, 但是 因为文件处理程序已经打开了,所以可能,只是可能, 在python中再次打开它可能会与同一个系统处理程序合并。在linux上试用过,效果不错

所以试着覆盖它

with open('program.exe', 'wb') as f:  # or 'w' for *.py extension
    f.write( your downloaded data )

它不起作用。我编辑了我的解释。我添加了代码。首先,您肯定应该将新程序命名为sys.argv[0]这是您通常遵循的程序更新过程:获取帮助程序可能作为资源包含在主程序中。将助手程序作为单独的进程运行。助手程序将更新下载到您的程序中。让主程序检测到更新已就绪,然后关闭自身或提示用户关闭它,因为更新已就绪。让助手检测原稿何时关闭,并用更新替换原稿。从帮助器启动更新版本,然后退出帮助器。更新后的版本现在可以删除助手了。我知道了。谢谢但有什么方法可以解决主程序上的这个问题吗?我得到了一个错误:IOError:[Errno 13]权限被拒绝