临时目录在程序运行期间持续存在

发布于 2024-12-25 20:40:44 字数 151 浏览 4 评论 0原文

我需要一个临时目录,但我想完全控制它的创建和删除。

我将使用这个目录来放置我想要监视新提交的 git 存储库,因此我需要将它们永久存储在某个地方。

因此我想避免 /tmp dir,因为它可以被用户清除(?)。对此的最佳实践是什么?

I need a temporary directory, but I want full control over its creation and deletion.

I will use this directory to place git repositories which I want to monitor for new commits, so I need to store them somewhere permanently.

Therefore I want to avoid /tmp dir, since it can be cleared by user(?). What is the best practice for this?

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

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

发布评论

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

评论(6

无戏配角 2025-01-01 20:40:44

tempfile.mkdtemp 将创建一个临时目录为你并返回它的名字。默认情况下(在类 Unix 系统上),它将在 /tmp 中创建它,但“以尽可能最安全的方式”,并且仅对调用者的用户 ID 具有读/写/列表权限。

>>> d = tempfile.mktemp()
>>> with open(os.path.join(d, "secret")) as output:
...     output.write("Ha, you can't read this!")

(顺便说一句,在具有默认设置的 Unix/Linux 系统上,用户不能直接在 /tmp 中编辑或删除彼此的文件。)

tempfile.mkdtemp will create a temp dir for you and return its name. It will create it in /tmp by default (on Unix-like systems), but "in the most secure manner possible" and with read/write/list permissions only for the caller's user id.

>>> d = tempfile.mktemp()
>>> with open(os.path.join(d, "secret")) as output:
...     output.write("Ha, you can't read this!")

(Btw., on a Unix/Linux system with default settings, users can't just edit or remove each others' files from /tmp.)

兲鉂ぱ嘚淚 2025-01-01 20:40:44

我想说最好的做法是使用 tempfile.mkdtemp< /代码>

如果您不想使用 /tmp 那么,您可以利用 prefix 参数:

import tempfile
tempfile.mkdtemp(prefix=<your_preferred_directory>)

编辑:关于存储应用程序配置、缓存的最合适目录是什么数据等。如果您使用的是 Linux,请查看 XDG 基本目录规范

I'd say that the best practice is to use tempfile.mkdtemp.

If you don't wan to use /tmp then, you can take advantage of the prefix parameter:

import tempfile
tempfile.mkdtemp(prefix=<your_preferred_directory>)

Edit: Regarding what's the most appropriate directory to sotre your application configuration, cache data, etc. If you're using linux, please have a look at the XDG Base Directory Specification.

甜味超标? 2025-01-01 20:40:44

如果它确实是临时的,请遵循 larmans 的建议并使用 mkdtemp

如果它是某种必须在重新启动后继续存在的半永久性缓存,那么您应该使用操作系统定义的本地应用程序目录(%APPDATA%、~/.local/ 等);一些工具包(例如Qt)提供了以跨平台方式查找该文件夹的功能。

编辑:来自维基百科:

  • HOME(类 Unix)和 USERPROFILE(Microsoft Windows)- 指示位置
    用户的主目录位于文件系统中。
  • HOME/{.AppName}(类 Unix)和 APPDATA{DeveloperName\AppName}
    (Microsoft Windows) - 用于存储应用程序设置。多开
    源程序错误地使用 USERPROFILE 进行应用程序设置
    在 Windows 中 - USERPROFILE 只能在允许的对话框中使用
    用户可以在文档/图片/下载/音乐等路径之间进行选择,
    出于编程目的,请使用 APPDATA(漫游)、LOCALAPPDATA 或
    PROGRAMDATA(在用户之间共享)

因此,您应该查找 os.environ['APPDATA']os.environ['HOME'],具体取决于平台(请参阅 sys.platform ),然后附加您的应用程序名称,然后您可以在其中存储您想要的任何内容。

mydir = os.path.join( ".myAppName", "cache")
homeVar = 'HOME'  # default for all *nix variants
if sys.platform == 'win32': 
   homeVar = 'APPDATA'
mydir = os.path.join( os.environ[homeVar], mydir)

If it's really temporary, follow larmans' advice and use mkdtemp.

If it's some sort of semi-permanent cache that must survive reboots, then you should use the local application directory, as defined by your OS (%APPDATA%, ~/.local/ etc); some toolkits (e.g. Qt) provide functions to look that folder up in a cross-platform manner.

Edit: from Wikipedia:

  • HOME (Unix-like) and USERPROFILE (Microsoft Windows) - indicate where
    a user's home directory is located in the file system.
  • HOME/{.AppName} (Unix-like) and APPDATA{DeveloperName\AppName}
    (Microsoft Windows) - for storing application settings. Many open
    source programs incorrectly use USERPROFILE for application settings
    in Windows - USERPROFILE should only be used in dialogs that allow
    user to choose between paths like Documents/Pictures/Downloads/Music,
    for programmatic purposes use APPDATA (roaming), LOCALAPPDATA or
    PROGRAMDATA (shared between users)

So you should look up os.environ['APPDATA'] or os.environ['HOME'], depending on platform (see sys.platform) and then append your app name, and then you can store there anything you want.

mydir = os.path.join( ".myAppName", "cache")
homeVar = 'HOME'  # default for all *nix variants
if sys.platform == 'win32': 
   homeVar = 'APPDATA'
mydir = os.path.join( os.environ[homeVar], mydir)
小矜持 2025-01-01 20:40:44

https://pypi.python.org/pypi/platformdirs 是一个 Python 包,提供跨平台user_cache_dir函数。

https://pypi.python.org/pypi/platformdirs is a Python package that offers a cross-platform user_cache_dir function.

捶死心动 2025-01-01 20:40:44

通常程序使用 ~/.progname 目录来存储应该持久但不妨碍用户的数据。

Usually programs use a ~/.progname directory to store data that should be persistent but should stay "out of the way" of the user.

め七分饶幸 2025-01-01 20:40:44

不过,您可能想研究一下 git commit hooks。这样,存储库就不会监视 tmp 目录中的新提交(听起来很奇怪:谁会以有限的权限提交到 tmp 目录中?),而是会通知您有关提交的信息,或者更具体地说,每当发生提交时就会自动运行脚本。

Just a though: You may want to look into git commit hooks. That way, instead of monitoring a tmp directory for new commits (sounds strange: who would commit into a tmp directory with limited permissions?) the repo informs you about commits, or, more specifically, automatically runs a script whenever a commit occurs..

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