简体   繁体   English

Win32 Errror,无法删除文件而不会出现错误

[英]Win32 Errror, can't delete files without errors

So my code is basically designed deletes a file if incorrect data is entered. 因此,我的代码基本上是设计用来在输入错误数据时删除文件的。 However it doesn't work, surprise surprise. 但是,它不起作用,令人惊讶。

Here's my code for the deleting segment: 这是我的删除段代码:

except ValueError:
        print("Something went wrong")
        file = open(username + ".txt", "w")
        file.close()
        import os, sys
        os.remove(username + ".txt")
        break

and this is the error that I retain: 这是我保留的错误:

WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'newtext.txt'

Any help would be great thanks :D 任何帮助将非常感谢:D

I think you have got the text file open in a word processor such as notepad. 我认为您已在文字处理器(如记事本)中打开了文本文件。 When running your Python script close the word processor beforehand and your code should work properly. 运行Python脚本时,请先关闭字处理器,然后代码应可以正常工作。 If this does not solve the problem then please give more details and I will try to help you to fix the problem. 如果这样做不能解决问题,请提供更多详细信息,我将尽力帮助您解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM