使用芹菜时没有这样的文件或目录错误

时间:2017-03-21 09:46:08

标签: python celery ioerror

这是我第一次使用Celery。我想做一个任务,它将搜索目录中的文件并进行处理。

到目前为止我的代码tasks.py

from celery import Celery
import os

app = Celery('tasks', broker='amqp://guest@localhost//')

path = 'the_files'

@app.task
def do_task_txt():
    for file in os.listdir(path):
        if file.endswith('.txt'):
                # string manipulation in the file
            with open(file) as f:
                lines = f.readlines()
                for line in lines:
                    #date = line[0:8]
                    #time = line[10:17]
                    timestamp = line[0:27].replace(" ", "")
                    data = line[35:].replace(" ", "")
                f.close()
    return timestamp, data

我在子目录/the_files中有一个文本文件。这是目录

的树
test_celery
    ├── tasks.py
    ├── the_files
           ├── test.txt

test.txt包含几行这样的

3/14/2009 20:08:24 1861400-6  <--- Receive Command:01010108 00606E422D4E
....
....

当我执行任务时,我运行了worker,它给了我以下错误

[2017-03-21 17:18:51,012: ERROR/PoolWorker-3] Task tasks.do_task_txt[1723fa86-30c2-40a3-accc-faef3a38c092] raised unexpected: IOError(2, 'No such file or directory')
Traceback (most recent call last):
 File "/Users/Fang/workspace/test_celery/venv/lib/python2.7/site-packages/celery/app/trace.py", line 367, in trace_task
    R = retval = fun(*args, **kwargs)
 File "/Users/Fang/workspace/test_celery/venv/lib/python2.7/site-packages/celery/app/trace.py", line 622, in __protected_call__
return self.run(*args, **kwargs)
  File "/Users/Fang/workspace/test_celery/tasks.py", line 14, in do_task_txt
with open(file) as f:
IOError: [Errno 2] No such file or directory: 'test.txt'

如果指向路径名称的错误,可能是我如何写出产生错误的路径的名称,但是当它指向文件名时呢?

感谢您的帮助和建议。

0 个答案:

没有答案