1

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.

1
  • How do you know it doesn't work? What if you use an absolute path instead of a relative one? Commented Dec 29, 2010 at 4:17

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

You can use import os; os.getcwd() to find out what directory Python thinks . refers to. This is often set to something very inconvenient; if I start up Python from my Start menu, it will be C:\\Windows\\system32 ! If I start it from a command window, it will be whatever the path was in the command window when I started up Python.
1

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

Comments

1
  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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.