【问题标题】:.sh to run two python files, One need the output of another.sh 运行两个 python 文件,一个需要另一个的输出
【发布时间】:2020-02-24 13:24:37
【问题描述】:

我遇到一个问题,我需要处理一个数据文件,我们将其命名为Data.data。我需要分两步处理这个数据文件,每一步都在一个单独的python.py文件中。

第一步(我们命名为first.py)将Data.data作为输入输出到一个名为output1.txt.txt文件中(output1.txt是由first.py生成的);然后第二步(我们将其命名为second.py),将output1.txt 作为输入并打印结果。

两个 python 文件都使用 sys.argv[1] 输入。

我正在尝试编写一个.sh 文件来完成上面列出的工作(Data.datafirst.pysecond.py 在同一个目录中)--- 第一个进程Data.datafirst.py ,这应该生成output1.txt,然后以output1.txt 作为输入运行second.py,并使用python 自己的print 函数打印结果。

谁能告诉我如何做到这一点?我花了很多时间尝试编写代码,但我真的被困住了。我可以写一个.sh同时运行2个.py但不喜欢这种情况,请救救我,非常感谢!

【问题讨论】:

  • 您能否展示到目前为止您在代码中所做的工作,以便轻松指出您卡在哪里?
  • 您能告诉我们或详细说明一下 first.py 和 second.py 中的内容吗? output1.txt 的读/写是硬编码的,还是要将文件名作为参数?

标签: python sh


【解决方案1】:

这个link 会有所帮助。

我试过这样做

我创建了两个文件 a.py 和 b.py

a.py

f= open("test.txt","w+")
for i in range(10):
 f.write("This is line %d\r\n" % (i+1))
f.close();

b.py

f=open("test.txt", "a+")
for i in range(2):
 f.write("Appended line %d\r\n" % (i+1))
f.close()

然后我有我的 shell 脚本

try.sh

#!/usr/bin/env bash
python a.py && python b.py

它首先按照文件 a.py 中的说明创建文件,然后按照文件 b.py 中的说明附加该文件。

【讨论】:

    猜你喜欢
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    相关资源
    最近更新 更多