2

我有一长串要处理的文件,在 python 编程的 10 个步骤中,我试图创建一个makefile来运行所有这些文件。由于某种原因,我不知道为什么,当我按照下面的代码运行它们时,第二步后输出为空。当我运行它们,一一调用时,代码可以工作,但是我在每一步中更改输出的名称,或者更改文件夹的输出,我不知道如何在makefiles. 我也接受其他建议makefiles(比如pypelines......我对shell命令有点迷茫)

我的makefile长相是这样的

zero::
        for f in `ls data/wikiMaths/*.html`; do \
        python src/maths/stripHtmlMaths.py $$f > $$f; \
        done

one::   
        for f in `ls data/wikiMaths/*.html`; do \
        python src/maths/Wiki2Text.py $$f > $$f; \
        done

three:: 
        for f in `ls data/wikiMaths/*.txt`; do \
        python src/maths/striphtml.py $$f > $$f; \
        done

four::
        for f in `ls data/wikiMaths/*.txt`; do \
        python src/maths/sipTrash.py $$f > $$f; \
        done

five::
        for f in `ls data/wikiMaths/*.txt`; do \
        python src/maths/tagFormula.py $$f > $$f; \
        done

six::
        for f in `ls data/wikiMaths/*.txt`; do \
        python src/maths/CountForm.py $$f > $$f; \
        done

seven::
        for f in `ls data/wikiMaths/*.txt`; do \
        python src/maths/stripWhiteSpace.py $$f > $$f; \
        done

eight::
        for f in `ls data/wikiMaths/*.txt`; do \
        python src/maths/Text2Xml.py $$f > $$f.xml; \
        done

nine::
        for f in `ls data/wikiMaths/*.xml`; do \
        python src/maths/Separate.py $$f > $$f; \
        done

我想做的是:

zero::
        for f in `ls data/wikiMaths/*.html`; do \
        python src/maths/stripHtmlMaths.py $$f > data/newFolder/$$f; \
        done

one::   
        for f in `ls data/newFolder/*.html`; do \
        python src/maths/Wiki2Text.py $$f > data/newFolderTwo/$$f.txt; \
        done

替换它:

python src/maths/stripHtmlMaths.py data/wikiMaths/file1.html > data/newFolder/file1.html
python src/maths/Wiki2Text.py data/newFolder/file1.html > data/newFolderTwo/file1.txt
4

2 回答 2

0

Python中的脚本比makefile更灵活你可以做类似的事情

import os
scriptFiles = ('script1.py', 'script2.py')
for scriptFile in scriptFiles:
    for _, _, files in os.walk(path): 
        for filename in files:
            exeLine = 'c:\\python27\\python.exe ' + scriptFile + ' ' + filename + ' > ' + filename + 'out'; os.system(exeLine)
        break # run os.walk(path) only once

您需要更改文件名,但我希望这个想法很清楚

于 2013-08-22T14:44:40.867 回答
0

我不明白您为什么根本不使用 Makefile 的强大功能;为什么不将每个文件的所有 9 个操作连接到 1 个配方中呢?或者为什么不用一个简单的 python 脚本来替换这一切。此外,您的管道必须一直截断源文件。

就像是

SOURCE_FILES := $(wildcard data/wikiMaths/*.txt)
TARGET_FILES := $(SOURCE_FILES:data/wikiMaths/%.txt=output/%.txt)

all: TARGET_FILES

$(TARGET_FILES): output/%: data/wikiMaths/%
        python src/process_from_source $< > $@
于 2013-08-22T14:41:34.793 回答