Python Forum
Redircet output of Python script to .txt file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Redircet output of Python script to .txt file
#1
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.
Reply
#2
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)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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.
Reply
#4
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).
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
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.
Reply
#7
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
If you use a version of Windows that implements the Linux version of bash, you can redirect the output to a file https://skorks.com/2009/09/output-redire...ags-plugin---
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,177 Jun-29-2023, 11:57 AM
Last Post: gologica
  Python Script to convert Json to CSV file chvsnarayana 8 2,489 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,088 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  How to modify python script to append data on file using sql server 2019? ahmedbarbary 1 1,214 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
Photo how I write the output into xml file in python? 3lnyn0 1 2,219 Oct-31-2021, 05:40 PM
Last Post: Gribouillis
  Showing and saving the output of a python file run through bash Rim 3 2,422 Oct-06-2021, 10:48 AM
Last Post: gerpark
  Real-Time output of server script on a client script. throwaway34 2 2,045 Oct-03-2021, 09:37 AM
Last Post: ibreeden
  Python script to summarize excel tables, then output a composite table? i'm a total n surfer349 1 2,334 Feb-05-2021, 04:37 PM
Last Post: nilamo
  Import output from python script to another varietyjones 1 1,895 Oct-12-2020, 09:07 PM
Last Post: bowlofred
  python script to Batch File biprabu 1 2,036 Sep-09-2020, 01:49 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020