Python: Getting AppData folder in a cross-platform way

前端 未结 4 1641
终归单人心
终归单人心 2021-02-12 21:03

I\'d like a code snippet that gets the proper directory for app data (config files, etc) on all platforms (Win/Mac/Linux at least). For example: %APPDATA%/ on Windows.

4条回答
  •  春和景丽
    2021-02-12 21:50

    I recommend researching the locations of 'appdata' in the operating systems that you want to use this program on. Once you know the locations you could simple use if statements to detect the os and do_something().

    import sys
    if sys.platform == "platform_value":
        do_something()
    elif sys.platform == "platform_value":
        do_something()
    
    • System: platform_value
    • Linux (2.x and 3.x): 'linux2'
    • Windows: 'win32'
    • Windows/Cygwin: 'cygwin'
    • Mac OS X: 'darwin'
    • OS/2: 'os2'
    • OS/2 EMX: 'os2emx'
    • RiscOS: 'riscos'
    • AtheOS: 'atheos'

    List is from the official Python docs. (Search for 'sys.platform')

提交回复
热议问题