Re: [Zope-dev] Extrenal method unable to run the os.popen() or os.system() commands
-----Original Message----- From: lists@zopyx.com Sent: Fri, 09 Feb 2007 09:51:37 +0100 To: wanster@inbox.com, zope-dev@zope.org Subject: Re: [Zope-dev] Extrenal method unable to run the os.popen() or os.system() commands
--On 9. Februar 2007 00:47:09 -0800 Ridzwan Aminuddin <wanster@inbox.com> wrote:
Hi guys...
I wrote an external python script which would grab some HTML and so some text and file preprocessing.
The script then is supposed to run come command line methods... using os.system() .. however when i run the script triggered by my DTML code it manages to do the grabbing of the html and saving it into a file ans stuff... but once it hits the portion where its supposed to run the command lines.. it just skips everything and goes right to the end.... after which this error message is shown :
Error Type IOError Error Value [Errno 2] No such file or directory: '/var/lib/zope2.8/instance/plone-site/Extensions/output.txt' Request made at
You can resolve this issue on your own by checking why the file is generated in the wrong place or by it is looked up at the wrong place..adding some debug code or using pdb should be sufficient to track this down.
-aj
Hi AJ! Sorry about that, i will revert to the users list. The problem with the script is that it does not execute the os.system() commands at all, not that it generates the file at the wrong place or anything. Do you know why this happens and how i can remedy it? I seem to have found an old thread of someone facing the same problem but the suggestion solution is not appropriate for my case. I have even made sure that the CLASSPATHS have been set to the directory holding my .jar file http://mail.zope.org/pipermail/zope-dev/2003-June/019918.html Thanks again! wan Here is my code : def removeIllegalChars( tgtString ): import os import sys from yahoo.search.news import NewsSearch import re tgtString = re.sub('<([^!>]([^>]|\n)*)>', '', tgtString) tgtString = re.sub('<([^>]([^>]|\n)*)>', '', tgtString) tgtString = re.sub('\n', '', tgtString) tgtString = re.sub('\t', '', tgtString) tgtString = re.sub('\r', '', tgtString) tgtString = re.sub(' ', '', tgtString) if tgtString: try: tgtString = str(tgtString) except UnicodeEncodeError: tgtString = str(tgtString.encode('U8')) return tgtString def getYahooAPI(): import os import sys from yahoo.search.news import NewsSearch import re queryWord = "Singapore" print ("Fetching data from YahooAPI with query : " + queryWord ) srch = NewsSearch('YahooDemo',query = "Singapore asean cup", language = 'en', results = 50) info = srch.parse_results() info.total_results_available homedir = os.getcwd() tgtFile = homedir + "/Extensions/InputString.txt" writeFile = open(tgtFile, 'w') for result in info.results: writeFile.write("<Article>\n") writeFile.write("<Title>\n") writeFile.write(removeIllegalChars(result['Title'])+ "\n") writeFile.write("<URL>\n") writeFile.write(removeIllegalChars(result['ClickUrl']) + "\n") writeFile.write("<Summary>\n") writeFile.write(removeIllegalChars(result['Summary']) + "\n") writeFile.write("<Date>\n") writeFile.write(removeIllegalChars(result['PublishDate']) + "\n") writeFile.write("<Source>\n") writeFile.write(removeIllegalChars(result['NewsSource']) + "\n") writeFile.write("<Body>\n") writeFile.write(removeIllegalChars(result['Summary']) + "\n") writeFile.write("<EndOfFile>") writeFile.close def runYahooSearch2(self): import os import sys from yahoo.search.news import NewsSearch import re #----------------------------------------------------------------------------------------------------------- # still trying to figure out how to get the absolute path to this script # commands below work for windows... but not for linux. Need to find a platform # independant one # print 'sys.argv[0] =', sys.argv[0] # homedir = os.path.dirname(sys.argv[0]) # for windows # print 'path =', pathname getYahooAPI() homedir = os.getcwd() homedir = homedir + "/Extensions" command = "cd "+homedir os.popen(command) command = "java -jar "+ '"' + homedir + "/" + "ActiveLearningTools.jar" + '" ' + "prepArticles >> test.txt" os.popen(command) print "Completed step 1 : preparing the articles" command = homedir + "/" + "vcluster " + "-zscores " + "ClutoMatrix" + " 2" sysCode = os.system(command) print "Completed step 2 : Clustering the articles using Cluto" command = "java -jar "+ '"' + homedir + "/" + "ActiveLearningTools.jar" + '" ' + "processClusters >> output.txt" sysCode = os.system(command) filename = homedir +"/output.txt" openfile = open(filename, 'r') whatisread = openfile.read() openfile.close() #os.remove("output.txt") return whatisread
Hi, I'm trying to run an external method that has a line that runs an os.system command to execute a Java .jar executable that simply outputs a text file as well as prints out a string. I've saved the python script as well as the Java .jar executable in my Extensions folder: /var/lib/zope2.8/instance/plone-site/Extensions/ Here is my Python script: def runYahooSearch2(self): import os homedir = os.getcwd() # on my machine homedir is /var/lib/zope2.8/instance/plone-site command = "java -jar "+ homedir + "/Extensions/test.jar" whatisread = os.popen(command).read() print whatisread This external method is triggered in my DTML code. I've created other external methods before so i know i've done it right.. but the os.system() command for java -jar just doesn't seem to work. I've tried to use other commands such as: os.popen("ls") and it works perfectly Its just this java -jar which does not execute at all... I know it doesn't execute at all cos it doesn't output a file or print the string. If i manually type out into the terminal : java -jar /var/lib/zope2.8/instance/plone-site/Extensions/test.jar it works perfectly But using the external method given above just gives me a blank output. I've also tried popen2: from popen2 import popen2 outputTGT, inputTGT = popen2(command) print outputTGT.readline() and still the jar is simply skipped and not executed. Does anyone know whats the cause and cure for this problem?... Or anynone knows any other method i can use instead of os.popen os.popen2 and os.system to run this command line statement?... or any way i can catch the exception which is being thrown when the java -jar is attempting to run ( if there is any in the firt place) ? Anything would be highly appreciated!... Kinda in a fix right now!... Thanks in advance! Cheers Wan
--On 12. Februar 2007 07:34:09 -0800 Ridzwan Aminuddin <wanster@inbox.com> wrote:
java -jar /var/lib/zope2.8/instance/plone-site/Extensions/test.jar
it works perfectly But using the external method given above just gives me a blank output.
I've also tried popen2:
from popen2 import popen2 outputTGT, inputTGT = popen2(command) print outputTGT.readline()
and still the jar is simply skipped and not executed.
You should see error messages on the console. Ensure you are running Zope in the foreground (zopectl fg). -aj
participants (2)
-
Andreas Jung -
Ridzwan Aminuddin