1

I am actually trying to write the commands and its output to a file and then read the file . The file is created , but for reading the file, it gives error as valueerror i/o operation. What am I missing here. ?

import pexpect
import time,sys
from StringIO import StringIO

telconn=pexpect.spawn('telnet 10.24.12.109')
telconn.logfile = sys.stdout
telconn.expect(":")
telconn.send("user" + "\r")
telconn.expect(":")
telconn.send("pass" + "\r\r\r\r\n\n\n")
telconn.expect("key to proceed.")
telconn.send ("\003")
telconn.expect("root>")
sys.stdout=open("test1.txt","w")

print "Telnet connection is done"

telconn.sendline('ls -al');
telconn.expect (['root>',pexpect.EOF])
ls = telconn.before

telconn.sendline('pwd');
telconn.expect (['root>',pexpect.EOF])
pwd = telconn.before

telconn.sendline('cli');
telconn.expect (['#',pexpect.EOF])
cli = telconn.before

telconn.sendline('\n\n');

telconn .sendline('exit');
telconn.close()

print ls
print pwd
print cli
print "Ended session"

sys.stdout.close()

sys.stdout = open("test1.txt", "r+")
str = sys.stdout.read();
print "Read String is : ", str
# Close opend file

print "Read String is : ", str
sys.stdout.close()
Suma
  • 241
  • 1
  • 2
  • 12
  • why use `sys.stdout` for reading? you can do `f = open("test1.txt", "r+")` and then `f.read()` – eli Feb 03 '16 at 04:44
  • thanks for the reply, I changed it to f for reading mode. But I am getting the below error Traceback (most recent call last): File "./sample.py", line 48, in ? print "Read String is : ", str ValueError: I/O operation on closed file – Suma Feb 03 '16 at 05:03
  • Did you change every `sys.stdout` to f? I recommend you try this. Just change `sys.stdout=open("test1.txt","w")` to `f=open("test1.txt","w")` and instead of printing use `f.write()` – eli Feb 03 '16 at 05:10
  • changing to f.write will not give the output of unix ls and pwd commands and it writes only ls and pwd in test1.txt file . sys.stdout will write the output of ls and pwd into test1.txt file – Suma Feb 03 '16 at 05:18

1 Answers1

1

edit (OP wants system call prints to file):

Save the standard output to some temp var, so just add:

prev_std = sys.stdout

before you do sys.stdout=open("test1.txt","w").

After you are done and closed sys.stdout, restore the original one:

sys.stdout = prev_std
eli
  • 490
  • 1
  • 3
  • 22
  • I understand that it is trying to write "Read String is : ", str" to a closed file. I want to know how can I write ls and pwd outputs to a file and then open that file , read it and print ? – Suma Feb 03 '16 at 05:27
  • I have edited my answer after you clarified your problem – eli Feb 03 '16 at 05:29
  • Thank you. this is serving my purpose. When I open the test1.txt file, it has ^M charector at every line, is there a way to avoid it in the file it writes ? – Suma Feb 03 '16 at 05:38
  • please refer to this: [What does ^M character mean in Vim](http://stackoverflow.com/questions/5843495/what-does-m-character-mean-in-vim), anyway I would appreciate if you accept my answer and upvote it – eli Feb 03 '16 at 05:46
  • Thanks Eli. I clicked on the check mark . i clicked on upvote, not sure if u r able to see it., – Suma Feb 03 '16 at 06:19
  • Also, if there are double tabs in the output, the file takes it as only single tab. how can we avoid it ? – Suma Feb 03 '16 at 09:03