用于建立“可写数据文件和C-Drive路径"的Python 3 OS命令是什么?典型的Windows 10应用程序 [英] What are the Python 3 os commands to establish "writable data files and C-Drive paths" for a typical Windows 10 Application
问题描述
为典型Windows 10应用程序建立可写数据文件和C-Drive路径"的Python 3 os命令是什么.这个问题有几个部分:
-
我的Python 3程序创建(多个)数据文件作为其目的的一部分.当我的MSI安装程序安装到/Programs时,我的Python可执行文件没有创建和写入数据文件的权限.因此,我的问题的第一部分是:是否需要更改Python 3程序以在特定目录中创建数据文件(使用os功能),您能举个例子吗?
-
我的问题的第二部分很简单:哪些os命令选项可以帮助我发现常规Windows 10 PC的Windows 10目录(例如,主路径,AppData路径等).
-
请注意,我正在cx_Freezing到MSI安装程序,因此对于MSI安装程序从云(谷歌驱动器或GitHub)进行的典型远程安装,必须自动进行一切操作,因此在回答1和2时请记住这一点.以上.
注意:这是此程序的MSI安装程序: 新的WINDOWS 10联系人管理应用程序.
https://drive.google.com/drive/folders/0Bz98wvqqw-1QRUNFcUJLU21yT1k
预先感谢您的编程知识和经验.
感谢您的技术帮助和澄清.
我建议您避免将文件和目录直接存储在用户的配置文件文件夹(即UserProfile
环境变量)或主文件夹(即"%HomeDrive%%HomePath%"
)中.这与Unix中用于主目录的常规做法不同(如果忽略了XDG基本目录规范),但是在Redmond中,请与Microsofties一样.
在以下一个或多个位置创建一个为应用程序唯一命名的文件夹:本地数据文件夹(按用户),漫游数据文件夹(按用户)或程序数据文件夹(按计算机) ).请注意,默认情况下这些文件夹是隐藏的,因为通常用户并不打算直接访问它们.
使用本地数据文件夹进行高速缓存.使用漫游数据文件夹获取有状态的用户数据和配置.将程序数据文件夹用于非特定于用户的数据和缓存.例如,像pip这样的程序可以使用程序数据文件夹来缓存下载的程序包. (实际上,pip为每个用户缓存程序包,但原则上可以为每台计算机缓存程序.)
如果您的应用程序使用程序数据文件夹,请确保该文件夹授予所有用户添加和修改子文件夹和文件的权限.如果延迟创建文件夹,则可以手动添加权限.有关如何修改文件安全性的示例,请参见此答案.
本地,漫游和程序数据文件夹的环境变量分别为LocalAppData
,AppData
和ProgramData
.在Windows XP中,后者是"%AllUsersProfile%\Application Data"
,也许应用程序数据"已本地化.通常,您不应该在应用程序中使用这些环境变量.
由于大多数已知/特殊文件夹在Explorer中都可以轻松重定位,因此最好通过调用SHGetFolderPath
或更新的SHGetKnownFolderPath
函数向外壳程序询问当前路径,而不是使用环境变量和默认位置.如果需要保留在Python的标准库中,则可以使用ctypes.但是使用PyWin32更容易,可以将其作为"pypiwin32"软件包进行pip安装.
以下是一些已知文件夹用于数据,文档和媒体的GUID文件:
User System
ProgramData FOLDERID_ProgramData
Local FOLDERID_LocalAppData
Roaming FOLDERID_RoamingAppData
Desktop FOLDERID_Desktop FOLDERID_PublicDesktop
Documents FOLDERID_Documents FOLDERID_PublicDocuments
Downloads FOLDERID_Downloads FOLDERID_PublicDownloads
Music FOLDERID_Music FOLDERID_PublicMusic
Pictures FOLDERID_Pictures FOLDERID_PublicPictures
Videos FOLDERID_Videos FOLDERID_PublicVideos
这里是相应的CSIDL
常量,除了下载"没有一个常量:
User System
ProgramData CSIDL_COMMON_APPDATA
Local CSIDL_LOCAL_APPDATA
Roaming CSIDL_APPDATA
Desktop CSIDL_DESKTOP CSIDL_COMMON_DESKTOPDIRECTORY
Documents CSIDL_PERSONAL CSIDL_COMMON_DOCUMENTS
Music CSIDL_MYMUSIC CSIDL_COMMON_MUSIC
Pictures CSIDL_MYPICTURES CSIDL_COMMON_PICTURES
Videos CSIDL_MYVIDEO CSIDL_COMMON_VIDEO
SHGetKnownFolderPath
不是由PyWin32包装的.我有另一个答案,它通过ctypes调用它.或者,您可以使用PyWin32创建 KnownFolderManager
实例.例如:
import pythoncom
from win32com.shell import shell
kf_mgr = pythoncom.CoCreateInstance(shell.CLSID_KnownFolderManager, None,
pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IKnownFolderManager)
downloads_path = kf_mgr.GetFolder(shell.FOLDERID_Downloads).GetPath()
或使用CSIDL
常量调用旧版SHGetFolderPath
函数.例如:
from win32com.shell import shell, shellcon
SHGFP_TYPE_CURRENT = 0
SHGFP_TYPE_DEFAULT = 1
local_data_path = shell.SHGetFolderPath(None, shellcon.CSIDL_LOCAL_APPDATA,
None, SHGFP_TYPE_CURRENT)
What are the Python 3 os commands to establish "writable data files and C-Drive paths" for a typical Windows 10 Application. There are several parts to this question:
My Python 3 program creates (multiple) data files as part of it's purpose. When my MSI installer installs into /Programs, my Python executable does not have permission to create and write data files. Thus, the first part of my question is: Do I need to change my Python 3 program to create data files in a specific directory (using the os capabilities) and could you give me an example.
The second part of my question is simply: What os command options can assist me in discovering the windows 10 directories of a general Windows 10 PC (e.g. the home path, the AppData path, etc).
Note that I am cx_Freezing to an MSI installer, so everything must be automated for a typical remote install from a cloud (google drive or GitHub) by the MSI installer, so keep that in mind when answering 1 and 2 above.
Attention: Here is the MSI Installer for this program: New WINDOWS 10 Contact Management Application.
https://drive.google.com/drive/folders/0Bz98wvqqw-1QRUNFcUJLU21yT1k
Thanks in advance for your programming knowledge and experience.
I appreciate your technical assistance and clarification.
I suggest that you avoid storing files and directories directly in the user's profile folder (i.e. the UserProfile
environment variable) or home folder (i.e. "%HomeDrive%%HomePath%"
). This differs from the common practice in Unix for a home directory (if we're ignoring the XDG base-directory spec), but when in Redmond, do as the Microsofties do.
Create a folder that's uniquely named for the application in one or more of the following locations: the local data folder (per-user), the roaming data folder (per-user), or the program data folder (per-machine). Note that these folders are hidden by default since generally users aren't meant to access them directly.
Use a local data folder for caches. Use a roaming data folder for stateful user data and configuration. Use a program data folder for data and caches that aren't specific to a user. For example, a program like pip could use the program data folder to cache downloaded packages. (In practice, pip caches packages per user, but in principle it could cache per machine.)
If your application uses a program data folder, make sure the folder grants all users permission to add and modify subfolders and files. If you create the folder lazily, you can add the permissions manually. See this answer for an example of how to modify file security.
The environment variables for the local, roaming, and program data folders are, respectively, LocalAppData
, AppData
and ProgramData
. In Windows XP the latter is "%AllUsersProfile%\Application Data"
, and maybe "Application Data" is localized. Generally you shouldn't use these environment variables in an application.
Since most known/special folders are easily relocatable in Explorer, it's best to ask the shell for the current path by calling SHGetFolderPath
or the newer SHGetKnownFolderPath
function instead of using environment variables and default locations. You can use ctypes for this if you need to stay within Python's standard library. But it's easier to use PyWin32, which can be pip installed as the "pypiwin32" package.
Here are some Known Folder GUIDs for data, documents, and media files:
User System
ProgramData FOLDERID_ProgramData
Local FOLDERID_LocalAppData
Roaming FOLDERID_RoamingAppData
Desktop FOLDERID_Desktop FOLDERID_PublicDesktop
Documents FOLDERID_Documents FOLDERID_PublicDocuments
Downloads FOLDERID_Downloads FOLDERID_PublicDownloads
Music FOLDERID_Music FOLDERID_PublicMusic
Pictures FOLDERID_Pictures FOLDERID_PublicPictures
Videos FOLDERID_Videos FOLDERID_PublicVideos
Here are the corresponding CSIDL
constants, except there isn't one for "Downloads":
User System
ProgramData CSIDL_COMMON_APPDATA
Local CSIDL_LOCAL_APPDATA
Roaming CSIDL_APPDATA
Desktop CSIDL_DESKTOP CSIDL_COMMON_DESKTOPDIRECTORY
Documents CSIDL_PERSONAL CSIDL_COMMON_DOCUMENTS
Music CSIDL_MYMUSIC CSIDL_COMMON_MUSIC
Pictures CSIDL_MYPICTURES CSIDL_COMMON_PICTURES
Videos CSIDL_MYVIDEO CSIDL_COMMON_VIDEO
SHGetKnownFolderPath
isn't wrapped by PyWin32. I have another answer that calls it via ctypes. Alternatively, you can use PyWin32 to create a KnownFolderManager
instance. For example:
import pythoncom
from win32com.shell import shell
kf_mgr = pythoncom.CoCreateInstance(shell.CLSID_KnownFolderManager, None,
pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IKnownFolderManager)
downloads_path = kf_mgr.GetFolder(shell.FOLDERID_Downloads).GetPath()
Or call the legacy SHGetFolderPath
function with a CSIDL
constant. For example:
from win32com.shell import shell, shellcon
SHGFP_TYPE_CURRENT = 0
SHGFP_TYPE_DEFAULT = 1
local_data_path = shell.SHGetFolderPath(None, shellcon.CSIDL_LOCAL_APPDATA,
None, SHGFP_TYPE_CURRENT)
这篇关于用于建立“可写数据文件和C-Drive路径"的Python 3 OS命令是什么?典型的Windows 10应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!