为什么我的 Python 代码不起作用?

发布于 2024-10-09 12:29:47 字数 434 浏览 0 评论 0原文

from celery.decorators import task

from celery.decorators import task
@task()
def add(x, y):
    r = open("./abc.txt","w")
    r.write("sdf")
    r.close()
    return x + y

这是我的tasks.py 文件。

>>> import tasks
>>> r = tasks.add.delay(3,5)
>>> r.result
8

如您所见,该函数有效。但是,文件不会创建。 为什么?

由于可能存在权限问题,我尝试更改多个文件路径。但没有运气。

from celery.decorators import task

from celery.decorators import task
@task()
def add(x, y):
    r = open("./abc.txt","w")
    r.write("sdf")
    r.close()
    return x + y

That's my tasks.py file.

>>> import tasks
>>> r = tasks.add.delay(3,5)
>>> r.result
8

As you can see, the function works. However, the file does not create.
Why?

I've tried changing multiple file paths, due to possible permission issues. but no luck.

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

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

发布评论

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

评论(3

诠释孤独 2024-10-16 12:29:47

如果文件没有被写入,您将得到一个异常,因此该函数不可能完成。

由于该函数返回 8,因此该文件正在被写入某处。

也许该文件被写入与您期望的目录不同的目录中

我能想到的唯一另一种可能性是正在运行的添加函数不是您在此处显示的函数

If the file was not being written, you would get an exception, so the function cannot possibly complete.

Since the function is returning 8, it follows that the file is being written somewhere.

Perhaps the file is written in a different directory to the one you are expecting

The only other possibility I can think of is that the add function that is being run is not the one that you have shown here

宁愿没拥抱 2024-10-16 12:29:47

我认为问题是你通过导入模块来运行它。文件路径中的 . 是相对于模块所在位置的,而不是当前工作目录。尝试给它一个完整的路径名。

如果这不起作用,请向我们准确显示您运行脚本的位置以及该目录上的 ls -la。如果仍然没有表现出任何异常的话。执行

find / -name abc.txt

I think the problem is that you're running this by importing a module. The . in the file path is relative to where the module lives, not your current working directory. Try giving it a full path name.

If that doesn't work, show us exactly where you're running the script from and an ls -la on that directory. And if that still doesn't show anything abnormal. Do a

find / -name abc.txt

我也只是我 2024-10-16 12:29:47
  1. 工作目录可能不是您期望的目录。
  2. 如果您使用多个主机,则该任务可以在另一台主机上完成。
  1. The working directory may not be the one you expect.
  2. The task may be done on another host, if you use multiple hosts.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文