【问题标题】:How to run python in a makefile and change the output name or directory?如何在 makefile 中运行 python 并更改输出名称或目录?
【发布时间】:2013-08-25 08:17:21
【问题描述】:

我有一长串要处理的文件,在 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

【问题讨论】:

    标签: python file-io makefile rename


    【解决方案1】:

    我不明白你为什么不使用 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 $< > $@
    

    【讨论】:

    • 我怎样才能将它们中的 9 个连接起来?...根据操作,我以行和其他方式访问文件作为一个文本,因此更改脚本我可以将文件读取为文本存储为列表在预览步骤中
    【解决方案2】:

    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
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 2021-04-26
      • 2021-12-02
      • 1970-01-01
      • 2013-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多