为什么我的 Python 代码不起作用?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果文件没有被写入,您将得到一个异常,因此该函数不可能完成。
由于该函数返回 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
我认为问题是你通过导入模块来运行它。文件路径中的
.
是相对于模块所在位置的,而不是当前工作目录。尝试给它一个完整的路径名。如果这不起作用,请向我们准确显示您运行脚本的位置以及该目录上的
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 afind / -name abc.txt