Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing terminal output to file

On my machine, I have some software which takes commands in the terminal and returns a list of values.

To run it, I have to type something like:

pdv -t filename

I am trying to run it as part of a python programme. When I run the following:

os.system('pdv -t %s' % (epoch_name))

then I get the values that I desire returned to my terminal (where epoch_name is the variable name for the filename). But when I try to write the result to a file:

os.system('pdv -t %s % "(epoch_name)" > 123.txt')

the file 123.txt is produced but it is empty.

I know that I am misplacing the " and/or ' characters, but I can't figure out where they should go.

Any help would be gratefully received!

like image 666
user1551817 Avatar asked Nov 18 '25 01:11

user1551817


1 Answers

You can use subprocess.call, with the stdout keyword argument:

import subprocess

cmd = ['ls', '-l']

with open('output.txt', 'w') as out:
    return_code = subprocess.call(cmd, stdout=out)
like image 120
Warren Weckesser Avatar answered Nov 20 '25 17:11

Warren Weckesser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!