临时目录在程序运行期间持续存在
我需要一个临时目录,但我想完全控制它的创建和删除。
我将使用这个目录来放置我想要监视新提交的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
tempfile.mkdtemp
将创建一个临时目录为你并返回它的名字。默认情况下(在类 Unix 系统上),它将在/tmp
中创建它,但“以尽可能最安全的方式”,并且仅对调用者的用户 ID 具有读/写/列表权限。(顺便说一句,在具有默认设置的 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.(Btw., on a Unix/Linux system with default settings, users can't just edit or remove each others' files from
/tmp
.)我想说最好的做法是使用
tempfile.mkdtemp< /代码>
。
如果您不想使用
/tmp
那么,您可以利用prefix
参数:编辑:关于存储应用程序配置、缓存的最合适目录是什么数据等。如果您使用的是 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 theprefix
parameter: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.
如果它确实是临时的,请遵循 larmans 的建议并使用
mkdtemp
。如果它是某种必须在重新启动后继续存在的半永久性缓存,那么您应该使用操作系统定义的本地应用程序目录(%APPDATA%、~/.local/ 等);一些工具包(例如Qt)提供了以跨平台方式查找该文件夹的功能。
编辑:来自维基百科:
用户的主目录位于文件系统中。
(Microsoft Windows) - 用于存储应用程序设置。多开
源程序错误地使用 USERPROFILE 进行应用程序设置
在 Windows 中 - USERPROFILE 只能在允许的对话框中使用
用户可以在文档/图片/下载/音乐等路径之间进行选择,
出于编程目的,请使用 APPDATA(漫游)、LOCALAPPDATA 或
PROGRAMDATA(在用户之间共享)
因此,您应该查找
os.environ['APPDATA']
或os.environ['HOME']
,具体取决于平台(请参阅 sys.platform ),然后附加您的应用程序名称,然后您可以在其中存储您想要的任何内容。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:
a user's home directory is located in the file system.
(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']
oros.environ['HOME']
, depending on platform (seesys.platform
) and then append your app name, and then you can store there anything you want.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.通常程序使用
~/.progname
目录来存储应该持久但不妨碍用户的数据。Usually programs use a
~/.progname
directory to store data that should be persistent but should stay "out of the way" of the user.不过,您可能想研究一下 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..