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)
Answer from Warren Weckesser on Stack OverflowYou 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)
I believe this does what you want. Argument to os.system() should be a string representing command to the OS.
os.system('pdv -t %s > 123.txt' % epoch_name)
There is subprocess module, which may worth look into if you are planning to process the output further in python.
how to get the stdout of the script that os.system executes - Post.Byes
linux - reading output into variables inside the os.system command - Unix & Linux Stack Exchange
How can I put all outputs that would appear in the terminal into a text file?
python - Save output of os.system to text file - Stack Overflow
Videos
os.system() just runs the process, it doesn't capture the output:
If command generates any output, it will be sent to the interpreter standard output stream.
The return value is the exit code of the process:
On Unix, the return value is the exit status of the process encoded in the format specified for wait().
You'll need to use something like subprocess.check_output() or subprocess.Popen() directly to capture the output.
>>> arch = subprocess.check_output("uname -a | awk '{print $9}'", shell=True);
>>> arch
'x86_64\n'
You can use subprocess module and achieve this fairly easy.
#!/usr/bin/python3
import subprocess
getVersion = subprocess.Popen("awk '{print $7}' /etc/redhat-release", shell=True, stdout=subprocess.PIPE).stdout
version = getVersion.read()
print("My version is", version.decode())
Also for the awk part, you can use sed 's/[^0-9_.-]//g /etc/redhat-release. This will only extract the version number and doesn't need to know what field version number is which makes it more platform independant.
I have a script running that prints a couple of lines then calls a different python function in a different directory which also prints something. This repeats about a 100 times.
Now I would like to direct all outputs of the terminal to a text file.
I've tried changing sys.stdout to an opened text file but that did not work.
Typing > file.txt and nohup Both resulted in having all the prints from the inner function first, then all the prints from the outer function.
I could just run it in the terminal but there has to be a better solution. Does anybody have an idea?
os.system executes the command in a subshell and returns the command's exit code. It does not provide any mean to capture the outputs of the command ("outputs" => what the command prints to it's stdout/stderr streams).
To capture the command's outputs you'll have to use some of the subprocess module's feature, the most obvious here being subprocess.check_output
# ...
import subprocess
# ...
# NB : you may want to catch subprocess.CalledProcessError here
out = subprocess.check_output(['adb', 'devices', '-l'])
msg = "{t}\nChecking for connected devices:\n{out}".format(t=t, out=out)
with open('logfile.txt', 'w') as f:
f.write(msg)
Try the following:
import os
import subprocess
import time
print ('Current Directory: {}'.format(os.getcwd()) )
print ('Opening Android SDK...')
os.chdir('C:\\android-sdk\\platform-tools')
print ('Current Directory: {}'.format(os.getcwd()) )
t = str(time.ctime())
try:
process_output = subprocess.check_output(["adb", "devices", "-l"])
except: #Add here the type of exception you want to raise and logic
print("Please check your ADB installation and syntax.")
s = ('{} Checking for connected devices: {}'.format(t,process_output) )
with open('logfile.txt', 'w') as f:
f.write(s)