如何修复工作目录始终是主目录? (Python)

发布于 2024-09-07 07:09:03 字数 661 浏览 12 评论 0原文

这是我的第一个问题。

我的 python 脚本使用以下简单函数打开并读取当前文本文件:

open("config.ini", "r")

因为这是一个相对路径,所以它应该可以工作,因为 config.ini 与脚本启动时放置在同一目录中,所以应该是当前工作目录。

事实上,这在我的所有 3 台 Linux 机器上都能完美运行,但我有一位用户需要支持,因为他在打开 config.ini 时遇到错误。会出现错误,因为

os.path.exists("config.ini")

即使文件存在也会返回 false!

试图解决这个问题,我们发现让它工作的唯一方法是将 config.ini 放在他的主目录中,尽管假定的工作目录是另一个目录。

另外,如果我的脚本尝试在当前工作目录中创建文件,则该文件始终会在他的主目录中创建,因此我认为由于某种原因,他的工作目录始终是主目录!

我该如何解决这个问题?也许我可以引入绝对路径,但我担心 os.getcwd() 会返回 homedir 而不是正确的目录。

我是否应该建议该用户以某种方式修复他的机器?

抱歉这个很长的问题,但英语不是我的母语,而且我是编码的初学者,所以解释起来有些困难。

预先非常感谢您! =)

This is my first question.

My python script opens and reads from a present text file using the following simple funct:

open("config.ini", "r")

As this is a relative path it is supposed to work because config.ini is placed in the same directory like the script is when it is launched, that should be the current working dir.

In fact this works perfectly on all of my 3 linux boxes, but I have one user who demands support because he gets an error while opening config.ini. The error raises because

os.path.exists("config.ini")

returns false even if the file is there!

Trying to fix this problem we found out that the only way to make it work is to place config.ini in his home directory despite the supposed working directory is another.

Also, if my script tries to create a file in the present working directory, the file is always created in his home dir instead, and so I think that for some reason his working dir is always home!

How can I troubleshoot this problem? Maybe I could introduce absolute paths, but I am afraid that os.getcwd() would return the homedir instead of the correct one.

Should I maybe suggest this user to fix his machine in some way?

Sorry for this long question but english is not my first language and I am a beginner in coding, so have some difficulties to explain.

Thank you very much in advance! =)

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

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

发布评论

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

评论(2

聚集的泪 2024-09-14 07:09:03

用户是否正在从他的主目录执行您的脚本?

即假设脚本位于:

/home/user/test/foo/foo.py

但用户这样调用它:

/home/user> python test/foo/foo.py

在这种情况下,脚本看到的“当前目录”是 /home/user

您可以做的是通过调用此函数找出脚本本身所在的目录:

import os

def script_dir():
    return os.path.dirname(os.path.realpath(__file__))

它将始终返回脚本所在的目录,而不是可能不同的当前目录。然后您可以安全地将配置文件存储在那里。

Could it be that the user is executing your script from his home directory?

I.e. suppose the script is in:

/home/user/test/foo/foo.py

But the user calls it thus:

/home/user> python test/foo/foo.py

In this case, the "current directory" the script sees is /home/user.

What you can do is find out the directory the script itself resides in by calling this function:

import os

def script_dir():
    return os.path.dirname(os.path.realpath(__file__))

It will always return the directory in which the script lives, not the current directory which may be different. You can then store your configuration file there safely.

红尘作伴 2024-09-14 07:09:03

与 Eli Bendersky 的建议相同,您可能想尝试一下:

os.path.exists(os.path.join(sys.path[0],"config.ini"))

因为 sys.path[0] 应始终是脚本所在的目录。

Along the same lines as Eli Bendersky's suggestion, you might want to try:

os.path.exists(os.path.join(sys.path[0],"config.ini"))

since sys.path[0] should always be the directory in which the script resides.

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