Greetings for the day.
I am a windows user. I am using Octave programs through Python and able to get the results of the octave program when I call it through Python script.
This code is for running the octave program in python:-
from oct2py import octave
import numpy as np
import os
out = octave.run('add.m')
Link to Octave Program 'add.m' :-
Octave Program
But I want to save the output of the Python script in .txt file. I am able to do that with Command prompt, but I want that my script will save the output in .txt file.
What should is needed to add in my Python script so that it will solve my problem?
Please help me.
Yes
from oct2py import octave
with open('out.txt', 'w') as fd:
fd.write(octave.run('add.m'))
There is also the possibility to print the output in your console (print function) and redirect the output in bash into a file.
If you want to print the output to console and save the text into a file:
from oct2py import octave
with open('out.txt', 'w') as fd:
out = octave.run('add.m')
print(out)
fd.write(out)
Thank you very much for your reply.
I have tried both your codes. When I run both codes TEXT file is created, but they are empty. In both cases error occurs which is shown below:
(May-03-2019, 11:07 AM)DeaD_EyE Wrote: [ -> ]Yes
from oct2py import octave
with open('out.txt', 'w') as fd:
fd.write(octave.run('add.m'))
There is also the possibility to print the output in your console (print function) and redirect the output in bash into a file.
If you want to print the output to console and save the text into a file:
from oct2py import octave
with open('out.txt', 'w') as fd:
out = octave.run('add.m')
print(out)
fd.write(out)
When I use this code
from oct2py import octave
with open('out.txt', 'w') as fd:
fd.write(octave.run('add.m'))
I got output with an error as shown below, but the text file is empty
Output:
c = 6
Error:
Traceback (most recent call last):
File "C:/Users/GURBHEJ SINGH/Desktop/beam/ADD.py", line 4, in <module>
fd.write(octave.run('add.m'))
TypeError: write() argument must be str, not None
When I use this code:
from oct2py import octave
with open('out.txt', 'w') as fd:
out = octave.run('add.m')
print(out)
fd.write(out)
I got output with an error as shown below, but the text file is empty
Output:
c = 6
None
Error:
Traceback (most recent call last):
File "C:/Users/GURBHEJ SINGH/Desktop/beam/ADD.py", line 6, in <module>
fd.write(out)
TypeError: write() argument must be str, not None
Please help me.
try:
octave.add()
or:
octave.feval("add.m") [
Source]
instead of:
octave.run('add.m')
Much better is, to read the source code:
https://github.com/blink1073/oct2py/blob...py/core.py
octave is an instance of the class Oct2Py. The is a __getattr__ method, which is called,
if a requested attribute does not exitst. In this case it's used as an import mechanism for .m files.
The requested attribute name is used to run the feval method. By the way, I could not find a run method in this class.
octave.run()
returns
None
.
Read more about the API and the
Examples from Oct2Py.
I guess your main program is called run.m. It should be in the path, if not, then add the path like in the example.
Quote:M-files in the directory where oct2py was initialized, or those in the Octave path, can be called like any other Octave function. To explicitly add to the path, use:
>>> from oct2py import octave
>>> octave.addpath('/path/to/directory')
The call to octave.run() should redirect the stdout of the program run.m (which runs in a octave-cli).
The
None
was returned, is a marker, that something went wrong.
Maybe the wrapper of the octave-cli returns None, if a call was made, but failed (file not found, syntax error in octave script).
what i have done many times, because i don't know if it is safe to replace sys.stdout, and because i am running on Linux, is open the file and get the file descriptor, and replace stdout at that layer like:
if fd!=1:
os.dup2(fd,1)
os.close(fd)
i usually sys.stdout.flush() before that for consistent output.
Greetings for the day.
Thank you for your efforts. I think their is no issue of path because all files are in same folder. I tried to do as you said but i end up with error.
(May-05-2019, 11:14 PM)DeaD_EyE Wrote: [ -> ]try:
octave.add()
or:
octave.feval("add.m") [Source]
instead of:
octave.run('add.m')
Error which i have got is as below
Error:
Traceback (most recent call last):
File "C:\Users\GURBHEJ SINGH\Desktop\multiple_inputs\oct2py1 - Copy.py", line 12, in <module>
result=octave.feval("multiple_inputs - Copy.m")
File "C:\Python\Python36\lib\site-packages\oct2py\core.py", line 373, in feval
store_as=store_as, plot_dir=plot_dir)
File "C:\Python\Python36\lib\site-packages\oct2py\core.py", line 572, in _feval
raise Oct2PyError(msg)
oct2py.utils.Oct2PyError: Octave evaluation error:
error: feval: function 'multiple_inputs - Copy' not found
@
Skaperen Thank you for the reply.
I am new with programming world so I do not clearly understand what are you trying to say.
(May-05-2019, 11:32 PM)Skaperen Wrote: [ -> ]what i have done many times, because i don't know if it is safe to replace sys.stdout, and because i am running on Linux, is open the file and get the file descriptor, and replace stdout at that layer like: if fd!=1:
os.dup2(fd,1)
os.close(fd)
i usually sys.stdout.flush() before that for consistent output.
I also try to search the web for the "descriptors", but it did not help to the extent that I understand the concepts behind the use of descriptors.
Please, can you explain more? I shall be very thankful to you.
it is an OS layer thing. i use Linux, not Windows, for my OS, and understand it very well. i have not tested that code on Windows. if you use Windows, do not use that code since it may not work as expected. this code is not a justified reason to change to Linux.