如何使用python找到真实的用户主目录?

发布于 2024-08-29 13:59:04 字数 622 浏览 6 评论 0 原文

我发现,如果我们更改 HOME (linux) 或 USERPROFILE (windows) 环境变量并运行 python 脚本,当我尝试时,它会返回新值作为用户主目录

os.environ['HOME']
os.exp

有没有办法不依赖环境变量找到真实的用户主目录?

编辑:
这是在 Windows 中通过读取注册表来查找 userhome 的方法,
http://mail.python.org/pipermail/python-win32 /2008-January/006677.html

编辑:
使用 pywin32 查找 Windows Home 的一种方法,

from win32com.shell import shell,shellcon
home = shell.SHGetFolderPath(0, shellcon.CSIDL_PROFILE, None, 0)

I see that if we change the HOME (linux) or USERPROFILE (windows) environmental variable and run a python script, it returns the new value as the user home when I try

os.environ['HOME']
os.exp

Is there any way to find the real user home directory without relying on the environmental variable?

edit:
Here is a way to find userhome in windows by reading in the registry,
http://mail.python.org/pipermail/python-win32/2008-January/006677.html

edit:
One way to find windows home using pywin32,

from win32com.shell import shell,shellcon
home = shell.SHGetFolderPath(0, shellcon.CSIDL_PROFILE, None, 0)

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

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

发布评论

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

评论(9

冷了相思 2024-09-05 13:59:04

我认为 os.path.expanduser(path) 可能会有所帮助。

在 Unix 和 Windows 上,返回参数,并将 ~~user 的初始组件替换为该用户的主目录。

在 Unix 上,如果设置了环境变量 HOME,则初始的 ~ 将被替换;否则,通过内置模块pwd在密码目录中查找当前用户的主目录。 直接在密码目录中查找初始~user

在 Windows 上,如果设置,将使用 HOME 和 USERPROFILE,否则将使用HOMEPATH 和 HOMEDRIVE 的组合初始~user是通过从上面派生的创建的用户路径中剥离最后一个目录组件来处理的

如果扩展失败或者路径不以波形符开头,则路径将原封不动地返回。

所以你可以这样做:

os.path.expanduser('~user')

I think os.path.expanduser(path) could be helpful.

On Unix and Windows, return the argument with an initial component of ~ or ~user replaced by that user‘s home directory.

On Unix, an initial ~ is replaced by the environment variable HOME if it is set; otherwise the current user’s home directory is looked up in the password directory through the built-in module pwd. An initial ~user is looked up directly in the password directory.

On Windows, HOME and USERPROFILE will be used if set, otherwise a combination of HOMEPATH and HOMEDRIVE will be used. An initial ~user is handled by stripping the last directory component from the created user path derived above.

If the expansion fails or if the path does not begin with a tilde, the path is returned unchanged.

So you could just do:

os.path.expanduser('~user')
我要还你自由 2024-09-05 13:59:04
from pathlib import Path

str(Path.home())

适用于 Python 3.5 及更高版本。 Path.home() 返回一个 Path 对象,提供 API 我发现非常有用。

from pathlib import Path

str(Path.home())

works in Python 3.5 and above. Path.home() returns a Path object providing an API I find very useful.

最单纯的乌龟 2024-09-05 13:59:04

我认为 os.path.expanduser(path) 是您问题的最佳答案,但在 Unix 世界中还有一个值得一提的替代方案:pwd 包。例如

import os, pwd

pwd.getpwuid(os.getuid()).pw_dir

I think os.path.expanduser(path) is the best answer to your question, but there's an alternative that may be worth mentioning in the Unix world: the pwd package. e.g.

import os, pwd

pwd.getpwuid(os.getuid()).pw_dir
〆凄凉。 2024-09-05 13:59:04

对于窗户;

import os
homepath = os.path.expanduser(os.getenv('USERPROFILE'))

将为您提供当前用户主目录的句柄,并将

filepath = os.path.expanduser(os.getenv('USERPROFILE'))+'\\Documents\\myfile.txt'

为您提供以下文件的句柄;

C:\Users\urUserName\Documents\myfile.txt

For windows;

import os
homepath = os.path.expanduser(os.getenv('USERPROFILE'))

will give you a handle to current user's home directory and

filepath = os.path.expanduser(os.getenv('USERPROFILE'))+'\\Documents\\myfile.txt'

will give you a handle to below file;

C:\Users\urUserName\Documents\myfile.txt
七婞 2024-09-05 13:59:04

home_folder = os.getenv('HOME')

这应该也适用于 Windows 和 Mac OS,在 Linux 上也适用。

home_folder = os.getenv('HOME')

This should work on Windows and Mac OS too, works well on Linux.

困倦 2024-09-05 13:59:04

事实上,环境变量的改变就意味着家必须改变。因此,每个程序/脚本都应该在上下文中拥有新家;后果也取决于改变它的人。
我仍然会坚持
home = os.getenv('USERPROFILE') 或 os.getenv('HOME')

到底需要什么?

Really, a change in environment variable indicates that the home must be changed. So every program/script should have the new home in context; also the consequences are up to the person who changed it.
I would still stick with
home = os.getenv('USERPROFILE') or os.getenv('HOME')

what exactly is required?

请叫√我孤独 2024-09-05 13:59:04

我意识到这是一个已经回答的老问题,但我想我会补充我的两分钱。接受的答案对我不起作用。我需要找到用户目录,并且希望它可以在使用或不使用 sudo 的情况下工作。在Linux中,我的用户目录是“/home/someuser”,但我的根目录是“/root/”。但是,在我的 Mac 上,用户目录是“/Users/someuser”。这是我最终所做的:

_USERNAME = os.getenv("SUDO_USER") or os.getenv("USER") 
_HOME = os.path.expanduser('~'+_USERNAME)

无论在 Mac 和 Linux 上使用还是不使用 sudo,这都有效。

I realize that this is an old question that has been answered but I thought I would add my two cents. The accepted answer was not working for me. I needed to find the user directory and I wanted it to work with and without sudo. In Linux, my user directory is "/home/someuser" but my root directory is "/root/". However, on my Mac, the user directory is "/Users/someuser". Here is what I ended up doing:

_USERNAME = os.getenv("SUDO_USER") or os.getenv("USER") 
_HOME = os.path.expanduser('~'+_USERNAME)

This worked both with and without sudo on Mac and Linux.

木格 2024-09-05 13:59:04

在 Linux 上获取(翻译的)用户文件夹名称:

from gi.repository import GLib

docs = GLib.get_user_special_dir(GLib.USER_DIRECTORY_DOCUMENTS)
desktop = GLib.get_user_special_dir(GLib.USER_DIRECTORY_DESKTOP)
pics = GLib.get_user_special_dir(GLib.USER_DIRECTORY_PICTURES)
videos = GLib.get_user_special_dir(GLib.USER_DIRECTORY_VIDEOS)
music = GLib.get_user_special_dir(GLib.USER_DIRECTORY_MUSIC)
downloads = GLib.get_user_special_dir(GLib.USER_DIRECTORY_DOWNLOAD)
public = GLib.get_user_special_dir(GLib.USER_DIRECTORY_PUBLIC_SHARE)
templates = GLib.get_user_special_dir(GLib.USER_DIRECTORY_TEMPLATES)

print(docs)
print(desktop)
print(pics)
print(videos)
print(music)
print(downloads)
print(public)
print(templates)

get (translated) user folder names on Linux:

from gi.repository import GLib

docs = GLib.get_user_special_dir(GLib.USER_DIRECTORY_DOCUMENTS)
desktop = GLib.get_user_special_dir(GLib.USER_DIRECTORY_DESKTOP)
pics = GLib.get_user_special_dir(GLib.USER_DIRECTORY_PICTURES)
videos = GLib.get_user_special_dir(GLib.USER_DIRECTORY_VIDEOS)
music = GLib.get_user_special_dir(GLib.USER_DIRECTORY_MUSIC)
downloads = GLib.get_user_special_dir(GLib.USER_DIRECTORY_DOWNLOAD)
public = GLib.get_user_special_dir(GLib.USER_DIRECTORY_PUBLIC_SHARE)
templates = GLib.get_user_special_dir(GLib.USER_DIRECTORY_TEMPLATES)

print(docs)
print(desktop)
print(pics)
print(videos)
print(music)
print(downloads)
print(public)
print(templates)
此生挚爱伱 2024-09-05 13:59:04

在 Linux 和其他 UNIXoid 上,您始终可以查看 /etc/passwd。主目录是其中第六个以冒号分隔的字段。但不知道如何比 Windows 上的环境变量做得更好。将会有一个系统调用它,但如果它可以从 Python 获得,...

On Linux and other UNIXoids you can always take a peek in /etc/passwd. The home directory is the sixth colon-separated field in there. No idea on how to do better than the environment variable on Windows though. There'll be a system call for it, but if it's available from Python, ...

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