Python 使用芹菜时没有此类文件或目录错误
这是我第一次用芹菜。我想做一个任务,它将搜索一个目录中的文件并处理它 迄今为止我的代码Python 使用芹菜时没有此类文件或目录错误,python,celery,ioerror,Python,Celery,Ioerror,这是我第一次用芹菜。我想做一个任务,它将搜索一个目录中的文件并处理它 迄今为止我的代码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.ends
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\u files
中有一个文本文件。这是目录的树
test_celery
├── tasks.py
├── the_files
├── test.txt
test.txt
包含如下几行
3/14/2009 20:08:24 1861400-6 <--- Receive Command:01010108 00606E422D4E
....
....
如果错误指向路径名,则可能是我如何编写产生错误的路径名,但当它指向文件名时又会怎样呢
感谢您的帮助和建议。尝试为其提供一条绝对路径。你的芹菜工人显然没有在“theu files”目录中查找“test.txt”。谢谢你的建议,但这不起作用。我将下面的行
path='the_files'
更改为path='/Users/Fang/workspace/test_芹菜/the_files'
,但它显示了相同的错误IOError:[Errno 2]没有这样的文件或目录:“test.txt”
@Fang我想知道您是否解决了这个问题。我面临着类似的问题。@Minigunr这是一个老问题,我不再从事这个项目,但正如我所记得的,我在执行芹菜命令时玩了路径。例如“celera-一个path1.path2.path3工作者”之类的东西。关于执行命令,您可以参考芹菜文档。@Fang谢谢您的回复。我想知道你在守护模式下使用芹菜后是否遇到了这个问题。如果我从终端运行芹菜,它会工作,但我的守护进程不工作。试着给它一个绝对路径。你的芹菜工人显然没有在“theu files”目录中查找“test.txt”。谢谢你的建议,但这不起作用。我将下面的行path='the_files'
更改为path='/Users/Fang/workspace/test_芹菜/the_files'
,但它显示了相同的错误IOError:[Errno 2]没有这样的文件或目录:“test.txt”
@Fang我想知道您是否解决了这个问题。我面临着类似的问题。@Minigunr这是一个老问题,我不再从事这个项目,但正如我所记得的,我在执行芹菜命令时玩了路径。例如“celera-一个path1.path2.path3工作者”之类的东西。关于执行命令,您可以参考芹菜文档。@Fang谢谢您的回复。我想知道你在守护模式下使用芹菜后是否遇到了这个问题。如果我从终端运行芹菜,它会工作,但我的守护进程不工作。
[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'