Python运行时目录

发布于 2024-07-21 10:55:40 字数 31 浏览 4 评论 0原文

如何从 Python 脚本中找到当前运行时目录?

How do I find out the current runtime directory from within a Python script?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧梦荧光笔 2024-07-28 10:55:40

使用 os.getcwd。 这将告诉您当前工作目录。 如果您使用创建新文件,

fp = open('a.txt', 'w')

则将在当前工作目录中创建 a.txt。 使用 os.chdir 更改工作目录(如果需要)。

Use os.getcwd. This will tell you the current working directory. If you create a new file with

fp = open('a.txt', 'w')

then a.txt will be created in the current working directory. Use os.chdir to change the working directory, if needed.

瑕疵 2024-07-28 10:55:40

这对我有用。

os.path.dirname(__file__)

This is working for me.

os.path.dirname(__file__)
寄风 2024-07-28 10:55:40

当您使用 os.chdir 时,os.getcwd() 将发生变化。

如果您需要 python 文件的固定路径,请使用:

module_path = os.path.dirname(__file__)

os.getcwd() will change when u use os.chdir

If you need a fixed path to your python file, use:

module_path = os.path.dirname(__file__)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文