Adding text for each line in getoutput() - python

I have a problem with figuring out how to add text for each line of getoutput method in python.
The code look like this:
...
def mloop():
for i in csv2hash(SLIST):
...
if i.get('IP'):
r = getoutput(CMD3 % (i['IP'])
for line in iter(r):
print i['IP']
...
The problem is that if I try this code, it will show me syntax error for a ":" in iter(r): and if i delete ":"(which i guess i should not do...) it will show syntax error for print. The CMD3 variable is a bash script for listing all files from folder on ssh server. And after all processing it all goes into csv (which code i did not include here)
I could just add IP in bash script, however I want to do this in python.
Thanks for help!

I think this code will solve your problem. Try this.
def mloop():
for i in csv2hash(SLIST):
...
if i.get('IP'):
r = getoutput(CMD3 % (i['IP']))
for line in iter(r):
print (i['IP'])

Related

Importing a function from a .py file

I work in a .ipybn file, but i want to import a function from a .py file.
My code is:
from function1 import my_function
However, I get the following error:
SyntaxError: unexpected EOF while parsing
How can I fix this? P.s the files are in the same folder.
You get the error when the file's source code ended before all the blocks in it are completed. For example, if in your file is:
a = input("> ")
if a == 'yes':
print("hello")
As you can see, you tell the program to proceed to print before the if statement is completed.
unexpected EOF while parsing
It was able to open the file, but not parse the content correctly. I would start by checking indentations (spaces vs tabs, # of spaces), quotes, colons.
Something to try is executing python from the command line and importing there. That will eliminate iPython/Jupyter notebook as a variable.

Can't get Python to put in loop variable into os.system command

Currently I am trying to automate a process between to computers, and one thing that needs to happen is a file transfer. For whatever reason, this line can't properly output the command I need it to.
for path in filePaths :
os.system('scp Host#host.IP:' + path + ' /save/file/here')
Any and all help is appreciated!
Probably something to do with spaces in path? Maybe try:
os.system('"scp {}#{}:{} {}"'.format(host, host_ip, path, save_path))

How to call a sed command in a python script?

Through python script, I am trying to sed command that through subprocess.call() as it in the script.
file = "a.xml"
updateData= "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=abc)(PORT=1234)))(CONNECT_DATA=(SERVICE_NAME=centraldb)))"
subprocess.call(["sed", "-i", 's#<DB_CONNECT_STRING>.*</DB_CONNECT_STRING>#<DB_CONNECT_STRING>updateData</DB_CONNECT_STRING>#', file])
When I run the command in the shell script or command, it runs fine, but in python I get a result saying "No input file". Any idea how to fix that error?
a.xml looks something like this.
<?xml version = '1.0' encoding = 'UTF-8'?>
<!DOCTYPE properties SYSTEM "java.sun.com/dtd/properties.dtd">
<properties> <!-- Database server details -->
<DB_CONNECT_STRING>(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=abc)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=cdb)))</DB_CONNECT_STRING>
</properties>
You really don't need or want to use an external subprocess for this.
import fileinput
updateData = "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=abc)(PORT=1234)))(CONNECT_DATA=(SERVICE_NAME=centraldb)))"
for line in fileinput.input('a.xml', inplace=True):
try:
prefix, tail = line.split('<DB_CONNECT_STRING>', 1)
_, suffix = tail.split('</DB_CONNECT_STRING>', 1)
line = prefix + '<DB_CONNECT_STRING>' + updateData + '</DB_CONNECT_STRING>' + suffix
except ValueError:
# <DB_CONNECT_STRING> or close tag not found -- don't replace
pass
print(line)
For the record, updateData inside quotes does not magically change into the value of the variable updateData so that was another problem with your attempt.
The ad-hoc XML processing is still a major wart; a proper solution would use an XML parser and perhaps XSLT to update the file. (On the other hand, if you know for a fact that the line will never contain anything outside the start and end tags, you can simplify the above script somewhat. On the third hand, the ad-hoc s-expressions inside the XML tags looks like you really want to rethink the configuration file format more thoroughly if you have any control at all over this.)

Tailing a log file

I want to add a log viewer tab to my website. The tab is supposed to print the whole log file, and after that print new lines (such as tail -F command in Linux) only. The client Side is in HTML and Javascript, and the server side is in Python.
Here is my tail Python function (I found it in the web):
#cherrypy.expose
def tail(self):
filename = '/opt/abc/logs/myLogFile.log'
f = subprocess.Popen(['tail','-F',filename],\
stdout=subprocess.PIPE,stderr=subprocess.PIPE)
p = select.poll()
p.register(f.stdout)
while True:
if p.poll(1):
print f.stdout.readline()
time.sleep(1)
This code is indeed printing the whole log file. However, each time I add new lines to the file, the file has been printed from the beginning, instead of printing the new lines.
Any suggestions how to fix it? I'm pretty new in Python, so I would appreciate any kind of help.
Check out pytailer
https://github.com/six8/pytailer
Specifically the follow command:
# Follow the file as it grows
for line in tailer.follow(open('/opt/abc/logs/myLogFile.log')):
print line

Local Blast empty xml file python

I am trying to implement a little script in order to automatize a local blast alignment.
I had ran commands in the terminal en it works perfectly. However when I try to automatize this, I have a message like : Empty XML file.
Do we have to implement a "system" waiting time to let the file be written, or I did something wrong?
The code :
#sequence identifier as key, sequence as value.
for element in dictionnaryOfSequence:
#I make a little temporary fasta file because the blast command need a fasta file as input.
out_fasta = open("tmp.fasta", 'w')
query = ">" + element + "\n" + str(dictionnary[element])
out_fasta.write(query) # And I have this file with my sequence correctly filled
OUT_FASTA.CLOSE() # EDIT : It was out of my loop....
#Now the blast command, which works well in the terminal, I have my tmp.xml file well filled.
os.system("blastn -db reads.fasta -query tmp.fasta -out tmp.xml -outfmt 5 -max_target_seqs 5000")
#Parsing of the xml file.
handle = open("tmp.xml", 'r')
blast_records = NCBIXML.read(handle)
print blast_records
I have an Error : Your XML file was empty, and the blast_records object doesn't exist.
Did I make something wrong with handles?
I take all advice. Thank you a lot for your ideas and help.
EDIT : Problem solved, sorry for the useless question. I did wrong with handle and I did not open the file in the right location. Same thing with the closing.
Sorry.
try to open the file "tmp.xml" in Internet explorer. All tags are closed?

Categories