sama
sama

Reputation: 79

How to write and then print a file in standard IO in Python?

I am newbie to Python and started writing scripts which helps me in my daily routine work. Recently, I wrote a SSH script which will login to a server and check the connectivity with a remote Application and print if it is successful.

I am able to achieve it, but I could not see the complete print of the File(which I am writing to) on standard IO. Can anyone please have a look in my code and suggest a way how it should be done?

My output from the code:-

*c:\Python27>python ssh1.py

['<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n', '</BODY></HTML>\n']

 HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n', '</BODY></HTML>\n']*

Not able to see the printout of "Site is up/down" although its written in the out.txt file.

ssh.connect('xx.xxx.xxx.xx', username= 'xxxxxx', password= 'xxxxx', port=22)
stdin, stdout, stderr = ssh.exec_command('wget -O- http://xx.xxx.xxx.xxx:7777 | grep "HTML"') #some application IP
f = open('out.txt', 'w+',100)
sys.stdout=f
print stdout.readlines()
print f.read()
if 'HTML' in f.read():
    print "Site is up"
else:
    print "Site is down"
output = os.system ("type c:\python27\out.txt")
print output
f.close()

Upvotes: 0

Views: 129

Answers (2)

Pandemonium
Pandemonium

Reputation: 8390

You opened the file but didn't attempt to write to it, thus an empty file. This is a simple reproduction using urllib's HTTP request, but otherwise the file I/O part is the same:

import urllib.request

# return a http.Request object
response = urllib.request.urlopen('http://google.com')                                                                           

# open the file for both reading and writing                                                                                                         
with open('out.txt', 'w+') as f:                                                                                       
    f.write(str(response.read())) 

    if 'HTML' in f.read():                                                                                                       
        print("Site is up")                                                                                                      
    else:                                                                                                                        
        print("Site is down") 

As a plus, by using with context manager to open a file, you do not have to worry about closing the file afterward.

Upvotes: 1

Eric Renouf
Eric Renouf

Reputation: 14490

You're assigning sys.stdout=f which means all future prints to stdout will end up in f. I'm really confused about why you're trying to make your local stdout go to a file, then trying to read from it--stdout is usually more of a "write only" stream for a program.

I think you might want to double check your logic, but once you do the assignment to sys.stdout that is where the output of all print statements will go.

Assuming the results aren't too huge and you have the memory for it you could do something like:

ssh.connect('xx.xxx.xxx.xx', username= 'xxxxxx', password= 'xxxxx', port=22)
stdin, stdout, stderr = ssh.exec_command('wget -O- http://xx.xxx.xxx.xxx:7777 | grep "HTML"') #some application IP
f = open('out.txt', 'w+',100)
result = stdout.read()
f.write( result )
if 'HTML' in result:
    print "Site is up"
else:
    print "Site is down"
output = os.system ("type c:\python27\out.txt")
print output
f.close()

This will read the results into a variable, print that to a file and the search it for your HTML keyword. You could also skip the os.system part by just printing result in my approach like

ssh.connect('xx.xxx.xxx.xx', username= 'xxxxxx', password= 'xxxxx', port=22)
stdin, stdout, stderr = ssh.exec_command('wget -O- http://xx.xxx.xxx.xxx:7777 | grep "HTML"') #some application IP
f = open('out.txt', 'w+',100)
result = stdout.read()
f.write( result )
if 'HTML' in result:
    print "Site is up"
else:
    print "Site is down"
print result

and you can actually simplify this a bit more, since you're already using grep to see if HTML is in the result, you don't have to test it on your side, you can just see if anything at all was returned like:

ssh.connect('xx.xxx.xxx.xx', username= 'xxxxxx', password= 'xxxxx', port=22)
stdin, stdout, stderr = ssh.exec_command('wget -O- http://xx.xxx.xxx.xxx:7777 | grep "HTML"') #some application IP
f = open('out.txt', 'w+',100)
result = stdout.read()
f.write( result )
if result:
    print "Site is up"
else:
    print "Site is down"
print result

If you really want your output file to contain a python list of strings of the lines you could do it like:

ssh.connect('xx.xxx.xxx.xx', username= 'xxxxxx', password= 'xxxxx', port=22)
stdin, stdout, stderr = ssh.exec_command('wget -O- http://xx.xxx.xxx.xxx:7777 | grep "HTML"') #some application IP
f = open('out.txt', 'w+',100)
result = stdout.readlines()
f.write(str(result))
if result:
    print "Site is up"
else:
    print "Site is down"
print result

Upvotes: 3

Related Questions