gpt4 book ai didi

python 将文件保存到用户文件夹(Windows)

转载 作者:可可西里 更新时间:2023-11-01 10:56:39 24 4
gpt4 key购买 nike

我有这个 Python 程序,它应该将文本文件保存到用户“我的文档”中的一个文件夹中。问题是程序不知道用户名,或者 Windows 版本不知道正确的路径。我怎样才能完成这项工作?

最佳答案

doc说,下面的代码也在 Windows 上扩展到用户的主目录..

homeDir = os.path.expanduser("~")

来自文档:

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.


另请参阅此以使用 Win32APIs

关于python 将文件保存到用户文件夹(Windows),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11174769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com