-1

I'm trying to iterate lines in a text file where each line is a command that I send to the command line to run. I want to keep track of each command and it's output by putting them in variables but i'm unsure how to assign a separate variable to each iteration. Here's my code to clarify.

example command.txt file:

echo "hello world"
echo "I am here"
ls

code to read and run the commands line by line

file = open("/home/user/Desktop" + "commands.txt", "r+")
lines = file.readlines()

for x in range(0,len(lines)):
    lines[x].strip()
    os.system(lines[x])
    # here I want to save each command to a different variable.
    # (for ex.) command_x = lines[x] (so you get command_1 = echo "hello world", and so on)
    # here I want to save the output of each command to a different variable.
    # (for ex.) output_x = (however I access the output...)

The reason I want to do this is so I can create a command log file where it will state the command given and the output. log file will look something like this:

Time/Date

command: echo "hello world"
output: hello world

command: echo "I am here"
output: I am here

command: ls
output: ... you get the point.
Daniel
  • 42,087
  • 4
  • 55
  • 81
BBEng
  • 155
  • 3
  • 17

3 Answers3

2

Saving each command is very easy. All we have to do is define our place to save the commands outside of the for loop. For example, we could make an empty list and append to that during each iteration.

commands = []
for x in range(0,len(lines)):
    lines[x].strip()
    os.system(lines[x])
    commands.append(lines[x])

In order to save the output, see this question and use another list outside of the for loop.

Additionally, you should read the file using

with open("/home/user/Desktop" + "commands.txt", "r+") as f:

and put all your other code within that block.

minterm
  • 259
  • 3
  • 13
1

Use a list to save the output. To save the output to a list, use subprocess.check_output:

with open("/home/user/Desktop/commands.txt") as lines:
    output = []
    for line in lines:
        output.append(subprocess.check_output(line.strip(), shell=True))

or with the command as a tuple:

with open("/home/user/Desktop/commands.txt") as lines:
    output = []
    for line in lines:
        line = line.strip()
        output.append((line, subprocess.check_output(line, shell=True)))
Daniel
  • 42,087
  • 4
  • 55
  • 81
  • I was just about to say that it would be good to save the command either as a `dict` or in a `tuple` along with the command output – MrJLP Sep 06 '17 at 17:42
0

You can do something along these lines:

import subprocess        
with open(fn) as f:
    for line in f:
        line=line.rstrip()
        # you can write directly to the log file:
        print "command: {}\noutput: {}".format(line, 
                        subprocess.check_output(line, shell=True)) 

If you want to save that in a list instead:

with open(fn) as f:
    list_o_cmds=[(line,subprocess.check_output(line, shell=True)) 
                          for line in (e.rstrip() for e in f)]       
dawg
  • 98,345
  • 23
  • 131
  • 206