如何从Python 3.3.2运行bat文件

时间:2014-06-30 13:54:44

标签: python batch-file

我正在用python开发一个程序,但是我有一点问题,因为我的程序需要的数据,它来自bat文件的结果,所以我想创建一些东西,我帮助我运行我的python中的脚本,不直接使用bat文件。

嗯,现在,我必须执行以下步骤:

  1. 我创建了一个txt文件,我在其中输入输入数据(例如:A.txt)
  2. 我使用文件A.txt
  3. 运行BAT文件
  4. 它创建一个新文件txt,其中包含运行BAT文件的结果(例如:B.txt)
  5. 我打开文件B.txt并复制所有数据然后将其粘贴到一个新的txt文件中,它将作为我的程序在python中的输入数据
  6. 我在python中运行我的程序
  7. 它会创建一个新文件txt,其中包含最终结果
  8. 结束
  9. 我是如何改进这一点的,我的意思是,我需要使用哪些脚本在Python中运行我的程序而不去Bat文件并执行上面描述的步骤?

1 个答案:

答案 0 :(得分:0)

我很确定你在bat文件中所做的一切,你可以在python模块中做。所以:

为您的* .bat

编写一个python模块

编写一个包含函数的python模块,用于读取文件A.txt并返回结果。然后使用程序中的该模块。

例如:

# calculations.py, the bat substitute.

def calculate_from_input(file_name):
    input_ = open(file_name, 'rb') # The mode you read the fike depends on your purposes.
    # .... Do your calculations
    return result   # Return a list of velocities, for example.

然后在你的主程序中

from calculations import calculate_from_input

calculated_data = calculate_from_input("A.txt")  # Then, you have the data ready to use in your program.
相关问题