强制删除文件。 Python、Windows

发布于 2024-11-19 15:26:44 字数 847 浏览 1 评论 0原文

我正在研究重置按钮。该按钮应该清除文本小部件中生成的文本并删除数据写入的文件。清除文本小部件可以,但我在删除文件时遇到问题

当我尝试从 Windows 中删除它时,它说它在 Python 中仍然打开。因此,我先从代码中关闭它,然后尝试删除它,但没有成功。

我已经对另一个按钮进行了编程,使其不会覆盖文件。所以,我不能只删除文件中已经写入的数据。

有什么办法可以强制删除该文件吗?

我正在使用Python 2.6.6。

这是我在重置按钮上的代码:

    def reset_it(self):
        self.run.update() # run is the other button, I have to stop it's processing!
        self.Outputlines.update() # Forcing the TextWidget to update.
        self.Outputlines.config(state=NORMAL) 
        self.Outputlines.delete(1.0, END) # Clearing everything from the TextWidget.
        self.Outputlines.config(state=DISABLED)
        self.run.wait_variable() # Stopping the run button.

        self.infile.close() # Closing the file.
        os.remove(self.filename.get()) # Code to delete the file.

I am working on a Reset button. The button is supposed to clear the text that is generated in a Text Widget and delete the file that the data was being written to. Clearing the Text Widget works, but I am having problems deleting the file.

When I try to delete it from windows, it says that it is still open in Python. So, I closed it from my code first, and then tried to delete it but didn't work.

I have programmed the other button so that it doesn't override files. So, I can't just delete the data that is already written in the file.

Is there any way that I can force the deletion of that file??

I am using Python 2.6.6.

This is my code on the reset button:

    def reset_it(self):
        self.run.update() # run is the other button, I have to stop it's processing!
        self.Outputlines.update() # Forcing the TextWidget to update.
        self.Outputlines.config(state=NORMAL) 
        self.Outputlines.delete(1.0, END) # Clearing everything from the TextWidget.
        self.Outputlines.config(state=DISABLED)
        self.run.wait_variable() # Stopping the run button.

        self.infile.close() # Closing the file.
        os.remove(self.filename.get()) # Code to delete the file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文