Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:以跨平台的方式获取AppData文件夹_Python_Appdata - Fatal编程技术网

Python:以跨平台的方式获取AppData文件夹

Python:以跨平台的方式获取AppData文件夹,python,appdata,Python,Appdata,我想要一个代码片段,它可以为所有平台(至少是Win/Mac/Linux)上的应用程序数据(配置文件等)获取正确的目录。例如:%APPDATA%/在Windows上。我建议您研究要在其上使用此程序的操作系统中“APPDATA”的位置。一旦知道了位置,就可以简单地使用if语句来检测操作系统并执行某些操作() 系统:平台价值 Linux(2.x和3.x):'linux2' Windows:“win32” Windows/Cygwin:“Cygwin” MacOSX:“达尔文” OS/2:“os2”

我想要一个代码片段,它可以为所有平台(至少是Win/Mac/Linux)上的应用程序数据(配置文件等)获取正确的目录。例如:%APPDATA%/在Windows上。

我建议您研究要在其上使用此程序的操作系统中“APPDATA”的位置。一旦知道了位置,就可以简单地使用if语句来检测操作系统并执行某些操作()

  • 系统:平台价值
  • Linux(2.x和3.x):'linux2'
  • Windows:“win32”
  • Windows/Cygwin:“Cygwin”
  • MacOSX:“达尔文”
  • OS/2:“os2”
  • OS/2 EMX:'os2emx'
  • 里斯科:“里斯科”
  • 阿瑟斯:“阿瑟斯”

列表来自。(搜索'sys.platform')

如果您不介意使用,它应该可以解决您的问题。(cost=您要么需要安装模块,要么直接将其包含在Python应用程序中。)

Qt列出了类似的路径

使用Python 3.8

import sys
import pathlib

def get_datadir() -> pathlib.Path:

    """
    Returns a parent directory path
    where persistent application data can be stored.

    # linux: ~/.local/share
    # macOS: ~/Library/Application Support
    # windows: C:/Users/<USER>/AppData/Roaming
    """

    home = pathlib.Path.home()

    if sys.platform == "win32":
        return home / "AppData/Roaming"
    elif sys.platform == "linux":
        return home / ".local/share"
    elif sys.platform == "darwin":
        return home / "Library/Application Support"

# create your program's directory

my_datadir = get_datadir() / "program-name"

try:
    my_datadir.mkdir(parents=True)
except FileExistsError:
    pass
导入系统 导入路径库 def get_datadir()->pathlib.Path: """ 返回父目录路径 可以存储持久应用程序数据的位置。 #linux:~/.local/share #macOS:~/Library/应用程序支持 #windows:C:/Users//AppData/漫游 """ home=pathlib.Path.home() 如果sys.platform==“win32”: 返回主页/“应用数据/漫游” elif sys.platform==“linux”: 返回主页/“.local/share” elif sys.platform==“darwin”: 返回主页/“库/应用程序支持” #创建程序的目录 my_datadir=get_datadir()/“程序名” 尝试: my_datadir.mkdir(parents=True) 除文件ExistError外: 通过
建议使用
sys.platform.startswith('linux')
“idiom”与返回“linux2”或“linux3”等内容的较旧版本的Python兼容。

您可以使用以下函数获取在linux和w10中测试的用户数据目录(返回
AppData/Local
dir),它是从包中改编的:

导入系统 从pathlib导入路径 从操作系统导入getenv def get_user_data_dir(应用名称): 如果sys.platform==“win32”: 导入winreg key=winreg.OpenKey( winreg.HKEY\u当前用户, r“软件\Microsoft\Windows\CurrentVersion\Explorer\Shell文件夹” ) dir_u,u=winreg.queryvaluex(键,“本地AppData”) ans=路径(dir)。解析(strict=False) elif sys.platform==“达尔文”: ans=路径(“~/Library/Application Support/”).expanduser() 其他: ans=Path(getenv('XDG\u DATA\u HOME',“~/.local/share”).expanduser() 返回ans.joinpath(appname)
这肯定只适用于Windows。我想要跨平台:)Python就是要把东西抽象到公共库中;为什么没有一个通用函数来执行此操作?我建议您遵循Linux的$XDG_DATA_HOME规范和Windows的%APPDATA%或%LOCALAPPDATA%规范,而不是硬编码。如果没有设置这些变量,Qt路径实际上是可以依赖的。一旦我的PR被合并,我将在明天添加一个指向示例代码的链接。^无法再编辑,但请点击这里:
import sys
import pathlib

def get_datadir() -> pathlib.Path:

    """
    Returns a parent directory path
    where persistent application data can be stored.

    # linux: ~/.local/share
    # macOS: ~/Library/Application Support
    # windows: C:/Users/<USER>/AppData/Roaming
    """

    home = pathlib.Path.home()

    if sys.platform == "win32":
        return home / "AppData/Roaming"
    elif sys.platform == "linux":
        return home / ".local/share"
    elif sys.platform == "darwin":
        return home / "Library/Application Support"

# create your program's directory

my_datadir = get_datadir() / "program-name"

try:
    my_datadir.mkdir(parents=True)
except FileExistsError:
    pass