1

I'm currently writing a program in python with pyCLIPS.

The clips module allows me to print multiple lines of output into the terminal simply by using: clips.PrintFacts()

However, I would like to output this to a file to save results. I am using the following code:

def Print():    
    f1=open('/var/log/combined/test.log', 'a')
    print >>f1, '- Facts -\n'
    print >>f1, clips.PrintFacts()
    print >>f1, '\n- Rules -\n'
    print >>f1, clips.Print.Rules()

the 1st and 3rd print commands successfully print their strings to the file but the 2nd and 4th print commands still only output the clips results into the terminal. Below is an example of the output:

============

root@ubuntu:/home/user/Desktop# python program.py
f-0    (initial-fact)
f-1    (duck)
f-2    (quack)
For a total of 3 facts.
MAIN:
Rule1
Rule2
Rule3
Rule4
Rule5
root@ubuntu:/home/user/Desktop# cat /var/log/combined/test.log
 - Facts -
None
 - Rules -
None
root@ubuntu:/home/user/Desktop#

============

The clips.PrintFacts() section starts at "f-0" whereas the clips.PrintRules() starts at "MAIN".

thanks in advance!

Amittai Shapira
  • 3,749
  • 1
  • 30
  • 54
Icey370
  • 15
  • 4
  • clips has its own i/o system. You need to figure out how to change the clips `stdout` stream; a quick look at the docs didn't turn up an example of this, so you may have to read the source. – cco Jan 27 '17 at 21:02

2 Answers2

1

Use a to append to the file:

f1 = open('/var/log/combined/test.log', 'a+')
print >>f1, clips.PrintFacts()

You are overwriting each line using w.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • thanks, it now adds additional lines whenever I run the program instead of just re-writing over the file. Unfortunately it's not capturing the output from clips.PrintFacts(). I'll have to try to find another way. Thanks for your help. – Icey370 Jul 27 '14 at 17:28
  • what does `print clips.PrintFacts()` output? – Padraic Cunningham Jul 27 '14 at 17:35
  • it outputs the current facts stored within the Expert System's (CLIPS) fact-base. clips.PrintFacts() directly prints to the command line, I just want a way of logging this output. – Icey370 Jul 27 '14 at 18:04
  • Add the exact output to your question – Padraic Cunningham Jul 27 '14 at 18:12
  • Added more info and the output I get. Sorry it took so long, I didn't know how to format the text properly. – Icey370 Jul 27 '14 at 18:49
  • if you print type( clips.PrintFacts()) what do you see? – Padraic Cunningham Jul 27 '14 at 19:14
  • changing `print >>f1. clips.PrintFacts()` and `print >>f1. clips.PrintRules()` to `print type(clips.PrintFacts())` and `print type(clips.PrintRules())` gets rid of the two "None" entries in the outputted file whilst also adding `` to the very last command line entry (after the output "Rule5") – Icey370 Jul 27 '14 at 19:25
  • I just wanted to see what `clips.PrintFacts()` was. Just a print statement in the code not actually writing to the file – Padraic Cunningham Jul 27 '14 at 19:28
  • States `` – Icey370 Jul 27 '14 at 19:31
  • if you `print clips.PrintFacts()` you definitely get output? – Padraic Cunningham Jul 27 '14 at 19:41
  • That's correct. It doesnt matter if the expert system is empty or not, there is always an output of some sort from `print clips.PrintFacts()`. – Icey370 Jul 27 '14 at 21:10
-1

(This answer is very spontaneous and I don't know if it's true, I just have an idea.)

I think it writes every line but then writes another line on top of it. You should save everything to a string (string = string + clips.printFacts()) and then save that to the file.

user3760874
  • 75
  • 1
  • 5