How to use subprocess to execute programs with Python -
hello using subprocess.popen() class , succesful execute commands on terminal, when try execute programs example script written on python , try pass arguments system fails.
this code:
argpath = "test1" args = open(argpath, 'w') if self.extract.getbyattr(self.block, 'name', 'args') != none: args.write("<request>"+self.extract.getbyattr(self.block, 'name', 'args')[0].toxml()+"</request>") else: args.write('') car = popen(shlex.split('python3.1 /home/hidura/webapps/karinapp/suite/foreingcode/savecss.py', stdin=args, stdout=subprocess.pipe, stderr=subprocess.pipe)) args.close() dataout = car.stdout.read().decode() log = car.stderr.read().decode() if dataout!='': return dataout.split('\n') elif log != '': return log.split('\n')[0] else: return none
and code savecss.py
from xml.dom.minidom import parsestring import os import sys class savcss: """this class has save changes on css file. """ def __init__(self, args): document = parsestring(args) request = document.firstchild address = request.getelementsbytagname('element')[0] newdata = request.getelementsbytagname('element')[1] cssfl = open("/webapps/karinapp/suite/"+address.getattribute('value'), 'r') cssdata = cssfl.read() cssfl.close() datacss = '' child in newdata.childnodes: if child.nodetype == 3: datacss += child.nodevalue nwcssdict = {} piece in datacss.split('}'): nwcssdict[piece.split('{')[0]] = piece.split('{')[1] cssdict = {} piece in cssdata.split('}'): cssdict[piece.split('{')[0]] = piece.split('{')[1] key in nwcssdict: if key in cssdict == true: del cssdict[key] cssdict[key] = nwcssdict[key] result = '' key in cssdict: result += key+"{"+cssdict[key]+"}" cssfl = open(cssfl.name, 'a') cssfl.write(result) cssfl.close() if __name__ == "__main__": savcss(sys.stdin)
btw: there's no output...
thanks in advance.
you should rather use communicate()
instead of .stdout.read()
.
and code posted isn't correct:
popen(shlex.split('python3.1 /home/hidura/webapps/karinapp/suite/foreingcode/savecss.py', stdin=args, stdout=subprocess.pipe, stderr=subprocess.pipe, shell=true)
there's missing parenthesis, , stdout
/stderr
parameters, it's clear no output console, rather pipes (if that's meant "there's no output...").
your code work on windows, on linux must remove shell=true
parameter. should omit parameter if provide full command line (as sequence).
Comments
Post a Comment