How do I find the Windows common application data folder using Python?

后端 未结 6 870
谎友^
谎友^ 2020-11-30 05:55

I would like my application to store some data for access by all users. Using Python, how can I find where the data should go?

6条回答
  •  臣服心动
    2020-11-30 06:10

    You can access all of your OS environment variables using the os.environ dictionary in the os module. Choosing which key to use from that dictionary could be tricky, though. In particular, you should remain aware of internationalized (i.e., non-English) versions of Windows when using these paths.

    os.environ['ALLUSERSPROFILE'] should give you the root directory for all users on the computer, but after that be careful not to hard code subdirectory names like "Application Data," because these directories don't exist on non-English versions of Windows. For that matter, you may want to do some research on what versions of Windows you can expect to have the ALLUSERSPROFILE environment variable set (I don't know myself -- it may be universal).

    My XP machine here has a COMMONAPPDATA environment variable which points to the All Users\Application Data folder, but my Win2K3 system does not have this environment variable.

提交回复
热议问题