Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.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
Python 用于重复执行命令行的脚本_Python_Bash_Shell_Command Line - Fatal编程技术网

Python 用于重复执行命令行的脚本

Python 用于重复执行命令行的脚本,python,bash,shell,command-line,Python,Bash,Shell,Command Line,我需要连续运行数百个命令行参数。尽管如此,对于我执行的每个命令,我都需要更改一个设置文件(文本文件) 例如,我需要运行“/program--settingsFile=blah.txt”,但每次我都需要更改设置文件中的一行文本 什么编程语言允许我: 读取包含我需要执行的命令的文件(.txt文件) 更改单独的设置文本文件 执行命令并等待命令完成 循环,直到列表 完整的 我在shell或python方面没有太多的知识,从我所读到的内容来看,这些似乎是实现这一点的最简单的方法 提前谢谢 您也可以使用

我需要连续运行数百个命令行参数。尽管如此,对于我执行的每个命令,我都需要更改一个设置文件(文本文件)

例如,我需要运行“/program--settingsFile=blah.txt”,但每次我都需要更改设置文件中的一行文本

什么编程语言允许我:

  • 读取包含我需要执行的命令的文件(.txt文件)
  • 更改单独的设置文本文件
  • 执行命令并等待命令完成
  • 循环,直到列表 完整的
我在shell或python方面没有太多的知识,从我所读到的内容来看,这些似乎是实现这一点的最简单的方法


提前谢谢

您也可以使用Python实现同样的功能

我已经为您的一个问题步骤编写了一个示例,您可以在其他步骤中使用并继续

#Reading a file from command line
import sys
with open(sys.argv[1], 'r') as f:
    contents = f.read()
print contents

我不知道你的代码是什么,但这里有一个通用模板

import subprocess
import os

while True: # this will make it run infinitely
    subprocess.run('path/to/program/file --settingsFile=blah.txt')
    settingsfile = open('path/to/settings/file', 'w+')
    settingsfilecontents = settingsfile.read()
    newtext = settingsfilecontents.replace('oldtext','newtext')
    settingsfile.write(newtext)
    settingsfile.close()
如果你不希望它无限期地运行,只需要做一个小的修改

import subprocess
import os
number = 100 #replace with times you want it to repeat
for i in range(number): # this will make it run number times
    subprocess.run('path/to/program/file --settingsFile=blah.txt')
    settingsfile = open('path/to/settings/file', 'w+')
    settingsfilecontents = settingsfile.read()
    newtext = settingsfilecontents.replace('oldtext','newtext')
    settingsfile.write(newtext)
    settingsfile.close()

你想让它运行特定的次数,还是无限次?几乎任何编程语言都会允许你这样做。这并不重要。您可以为bash文件或python文件创建cron作业。如果它无限运行,您可以在后台运行一个进程,同样,bash或python……是的,他要求从上述两种脚本中获得最佳效果。