在OS X上获取当前用户的主目录? [英] Getting current user home directory on OS X?

查看:111
本文介绍了在OS X上获取当前用户的主目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在OS X上查找当前用户的主目录?

How to find current user home directory on OS X?

HOME环境变量并非总是设置的,例如,当您不在控制台(GUI应用程序)中运行时.

HOME environmental variable is not always set, for example when you do not run in console (GUI apps).

因此,我正在寻找一种通用的解决方案,如果未设置os.environ['HOME'],它将回退.

For this reason I'm looking for a generic solution, one that will fall-back if os.environ['HOME'] is not set.

有一个类似的问题(C),但是它已经有一个无效的可接受的解决方案.

There is a similar question (C) but it already has an accepted solution that is invalid.

将首选Python解决方案,但也欢迎使用其他语言,只要它们提供有效的住所或至少一个您可以编写的位置即可.

A Python solution would be preferred but other languages are welcome as long they provide a valid home or at least one location where you can write.

推荐答案

即使在Windows上,os.path.expanduser("~")总是返回主目录.

It looks that os.path.expanduser("~") always returns the home directory, even on Windows.

这篇关于在OS X上获取当前用户的主目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆