在Python中获取临时目录的跨平台方法

发布于 2024-07-19 06:21:08 字数 190 浏览 4 评论 0原文

有没有跨平台的方法来获取 Python 2.6 中 temp 目录的路径?

例如,在 Linux 下为 /tmp,而在 XP 下为 C:\Documents and settings\[user]\Application settings\Temp

Is there a cross-platform way of getting the path to the temp directory in Python 2.6?

For example, under Linux that would be /tmp, while under XP C:\Documents and settings\[user]\Application settings\Temp.

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

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

发布评论

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

评论(5

鼻尖触碰 2024-07-26 06:21:08

那将是 tempfile 模块。

它具有获取临时目录的功能,并且还具有一些在其中创建临时文件和目录的快捷方式,命名或未命名。

示例:

import tempfile

print tempfile.gettempdir() # prints the current temporary directory

f = tempfile.TemporaryFile()
f.write('something on temporaryfile')
f.seek(0) # return to beginning of file
print f.read() # reads data back from the file
f.close() # temporary file is automatically deleted here

为了完整起见,以下是它如何根据 搜索临时目录文档

  1. TMPDIR 环境变量命名的目录。
  2. TEMP 环境变量命名的目录。
  3. TMP 环境变量命名的目录。
  4. 特定于平台的位置:
    • 在 RiscOS 上,由 Wimp$ScrapDir 环境变量命名的目录。
    • 在 Windows 上,目录 C:\TEMPC:\TMP\TEMP\TMP,按此顺序。
    • 在所有其他平台上,目录按顺序为 /tmp/var/tmp/usr/tmp。< /里>
  5. 作为最后的手段,当前工作目录。

That would be the tempfile module.

It has functions to get the temporary directory, and also has some shortcuts to create temporary files and directories in it, either named or unnamed.

Example:

import tempfile

print tempfile.gettempdir() # prints the current temporary directory

f = tempfile.TemporaryFile()
f.write('something on temporaryfile')
f.seek(0) # return to beginning of file
print f.read() # reads data back from the file
f.close() # temporary file is automatically deleted here

For completeness, here's how it searches for the temporary directory, according to the documentation:

  1. The directory named by the TMPDIR environment variable.
  2. The directory named by the TEMP environment variable.
  3. The directory named by the TMP environment variable.
  4. A platform-specific location:
    • On RiscOS, the directory named by the Wimp$ScrapDir environment variable.
    • On Windows, the directories C:\TEMP, C:\TMP, \TEMP, and \TMP, in that order.
    • On all other platforms, the directories /tmp, /var/tmp, and /usr/tmp, in that order.
  5. As a last resort, the current working directory.
够钟 2024-07-26 06:21:08

这应该可以满足您的要求:

print(tempfile.gettempdir())

对于我的 Windows 机器,我得到:

c:\temp

在我的 Linux 机器上,我得到:

/tmp

This should do what you want:

print(tempfile.gettempdir())

For me on my Windows box, I get:

c:\temp

and on my Linux box I get:

/tmp
南七夏 2024-07-26 06:21:08

我使用:

from pathlib import Path
import platform
import tempfile

tempdir = Path("/tmp" if platform.system() == "Darwin" else tempfile.gettempdir())

这是因为在 MacOS 上,即 Darwin,tempfile.gettempdir()os.getenv('TMPDIR') 返回一个值,例如 '/ var/folders/nj/269977hs0_96bttwj2gs_jhhp48z54/T'; 这是我并不总是想要的。

I use:

from pathlib import Path
import platform
import tempfile

tempdir = Path("/tmp" if platform.system() == "Darwin" else tempfile.gettempdir())

This is because on MacOS, i.e. Darwin, tempfile.gettempdir() and os.getenv('TMPDIR') return a value such as '/var/folders/nj/269977hs0_96bttwj2gs_jhhp48z54/T'; it is one that I do not always want.

彩扇题诗 2024-07-26 06:21:08

简单的方法,基于@nosklo的评论和答案

import tempfile
tmp = tempfile.mkdtemp()

但是如果你想手动控制目录的创建:

import os
from tempfile import gettempdir
tmp = os.path.join(gettempdir(), '.{}'.format(hash(os.times())))
os.makedirs(tmp)

最 当您完成后(出于隐私、资源、安全等考虑),您可以轻松地自行清理:

from shutil import rmtree
rmtree(tmp, ignore_errors=True)

这类似于 Google Chrome 和 Linux systemd 等应用程序的做法。 他们只是使用较短的十六进制哈希值和特定于应用程序的前缀来“宣传”他们的存在。

The simplest way, based on @nosklo's comment and answer:

import tempfile
tmp = tempfile.mkdtemp()

But if you want to manually control the creation of the directories:

import os
from tempfile import gettempdir
tmp = os.path.join(gettempdir(), '.{}'.format(hash(os.times())))
os.makedirs(tmp)

That way you can easily clean up after yourself when you are done (for privacy, resources, security, whatever) with:

from shutil import rmtree
rmtree(tmp, ignore_errors=True)

This is similar to what applications like Google Chrome and Linux systemd do. They just use a shorter hex hash and an app-specific prefix to "advertise" their presence.

离笑几人歌 2024-07-26 06:21:08

为什么有这么多复杂的答案?

我只用这个

   (os.getenv("TEMP") if os.name=="nt" else "/tmp") + os.path.sep + "tempfilename.tmp"

Why so many complex answers?

I just use this

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