如何删除Python 3中的3个搁置文件?

发布于 2024-10-16 16:33:55 字数 289 浏览 2 评论 0原文

I wrote a few unittests with shelve at http://code.google.com/p/filecache/ and python 2 saves exactly the filename I specifiy in shelve.open() but in python 3 I get 3 different files "bak", "dat" and "dir". So before the tests start I want to erase these files but I don't know if I have any guarantee as to their filename or extension.

How can I erase a shelve if I know it's name?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酸甜透明夹心 2024-10-23 16:33:55

您获得的扩展取决于使用的数据库后端。 Python 2 和 Python 3 之间的默认值可能有所不同,但也可能是您的环境中可用的数据库接口之间存在差异。

因此,不,您无法保证扩展,除非您使用特定的实现,即 BsdDbShelf 或 DbfilenameShelf。您可以在 tempfile 创建的临时目录中指定一个文件,然后删除 while 目录。

What extensions you get depends on which database backend is used. It's possible that the default differs between Python 2 and Python 3, but it can also be a difference between what database interfaces are available in your environment.

So no, you don't have a guarantee to the extensions, unless you use a specific implementation, ie either BsdDbShelf or DbfilenameShelf. You could probably specify a file in a temporary directory created by tempfile, and then delete the while directory.

情徒 2024-10-23 16:33:55

我使用 shelve 因为 tempFile 和 dict[] 对象无法跨模块持久化。正如您所发现的,调用 .clear() 不会清除磁盘上持久对象的内容,退出后会在磁盘上留下填充的 r+w 文件。 (类似于释放后使用漏洞)您可以在使用完毕后删除搁置:

import os
import shelve

shelve_name = 'shelve_name'
shelve_contents = shelve.open(shelve_name, flag='c', protocol=None, writeback=False)

shelve_file = (os.path.join(os.getcwd(), shelve_name))
os.remove(shelve_file)

I use shelve because tempFile and dict[] objects cannot persist across modules. As you have discovered, calling .clear() does not clear content from the persistent object on disk, leaving a populated r+w file on disk after exit. (Similar to a use-after-free vulnerability) You can delete the shelve when finished using:

import os
import shelve

shelve_name = 'shelve_name'
shelve_contents = shelve.open(shelve_name, flag='c', protocol=None, writeback=False)

shelve_file = (os.path.join(os.getcwd(), shelve_name))
os.remove(shelve_file)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文