如何在主操作系统路径(而不是pythonpath)上找到文件?

发布于 2025-01-23 07:09:54 字数 731 浏览 4 评论 0原文

如何使用Python找到文件在系统路径上的文件位置(即Windows%路径%环境变量)?

这与python路径,内置sys.path或环境变量pythonpath,而是关于终端中存在的主要操作系统的路径,例如cmd,bash等。

首选项:

  1. 直接python代码(而不是运行shell命令)
  2. 基于python 3的解决方案,使用 pathlib.path.path
  3. platform-

notes- notes notes

  1. i'i' m不想只是获取路径(即os.environ ['path']),而是在路径上找到特定文件的解决方案,类似于Windows where < /code>命令或linux 哪个命令,但使用python代码。
  2. 我希望找到任何文件,而不仅仅是可执行文件(理想情况下,您可以放入文件模式并获得所有结果),不同于 this Q&amp; A

How can I find a file the location of a file on the System PATH (i.e. in Windows %PATH% environment variable) with Python?

This is not about the Python Path, the built-in sys.path, or the environment variable PYTHONPATH, but the PATH from the main OS that is present in terminals such as CMD, bash, etc.

Preferences:

  1. Direct Python code (rather than running a shell command)
  2. A Python 3-based solution, using pathlib.Path
  3. Platform-independent

Notes

  1. I'm not looking to just get the PATH (that is just os.environ['PATH']), but a solution to find a particular file on the PATH, similar to the Windows where command, or linux which command, but using Python code.
  2. I'm looking to find any file, not just executables (ideally you could put in a file pattern and get all results), different from this Q&A.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文