Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pythondoit:使用从上一个任务生成的文件创建子任务_Python_Python 3.x_Doit - Fatal编程技术网

pythondoit:使用从上一个任务生成的文件创建子任务

pythondoit:使用从上一个任务生成的文件创建子任务,python,python-3.x,doit,Python,Python 3.x,Doit,使用,我希望通过读取文件(在以前的任务中生成)中的文件列表来生成子任务,然后为每个文件生成操作: def task_generates_list_of_files(): def generate_file(): with open('list_of_files.txt', 'w') as fo: for x in range(20): fo.write(f'thing_{x}.txt\n') retu

使用,我希望通过读取文件(在以前的任务中生成)中的文件列表来生成子任务,然后
为每个文件生成操作:

def task_generates_list_of_files():

    def generate_file():

        with open('list_of_files.txt', 'w') as fo:

            for x in range(20):
                fo.write(f'thing_{x}.txt\n')

    return {
        'actions': [generate_file],
        'targets': ['list_of_files.txt']
    }

def task_generate_subtasks_using_file():

    with open('list_of_files.txt', 'r') as fo:

        for filename in fo:

            filename = filename.strip()

            yield {
                'name': filename,
                'actions': [f'program_to_run {filename}'],
                'file_dep': ['list_of_files.txt']
            }
但是,由于doit设置任务时,
list\u of_files.txt
不存在,因此会引发
FileNotFoundError
异常

我已经看到但不清楚
getargs
在生成子任务时是否可以工作,因为在将文件注入Python操作之前,我无法循环遍历文件列表,此时我无法生成任何子任务。这样做的结果是:

Python Task error:...
It must return:
False for failed task.
True, None, string or dict for successful task
returned <generator object...
Python任务错误:。。。 它必须返回: 对于失败的任务,为False。 True、None、string或dict表示任务成功
最后返回,我错过了doit文档中以前没有看到的东西:。您可以使用
create\u after
将任务的创建推迟到给定任务执行之后

在我的例子中,这允许在
list\u of_files.txt
task\u generate\u子任务的任务定义中使用
读取
task\u generate\u子任务之前,通过
task\u生成
来生成
list\u of_files.txt:

from doit import create_after

# Show output
DOIT_CONFIG = {
    'verbosity': 2
}

def task_generates_list_of_files():

    def generate_file():

        with open('list_of_files.txt', 'w') as fo:

            for x in range(20):
                fo.write(f'thing_{x}.txt\n')

    return {
        'actions': [generate_file],
        'targets': ['list_of_files.txt']
    }

@create_after('generates_list_of_files')
def task_generate_subtasks_using_file():

    with open('list_of_files.txt', 'r') as fo:

        for filename in fo:

            filename = filename.strip()

            yield {
                'name': filename,
                'actions': [f'echo {filename}'],
                'file_dep': ['list_of_files.txt']
            }

返回的
我理解这一点,但问题特定于Python
doit
(),您可以使任务函数返回包含doit要运行的任务的字典,或者使用
yield
生成“子任务”。但是,我无法在
doit
运行脚本时运行的任务定义中打开我的
list\u of_files.txt
,因为它在第一个任务运行之前不存在。第二部分是解释我不能使用方法在
doit
任务(称为
getargs
)之间传递值,因为我不能
从任务()内的函数中产生
子任务。请看,这会重现问题,然后,有人可能会向您展示如何使用
getargs
来完成您想要做的事情
pip3安装doit
,然后将该代码放入
dodo.py
,然后在文件夹中运行
doit
,它会产生相同的错误。上面的标记没有帮助,因为这与其说是一个Python问题,不如说是一个
doit
问题,而且我没有足够的rep来创建一个新标记。我理解你所说的,作为Python,除了定义两个函数外,它什么都不会做,但是使用
doit
那些
task_u
函数是用来定义任务的,失败是因为第二个任务定义在第一个任务之前运行,但取决于第一个任务的输出。